wp_cache_set example

$alloptions = wp_load_alloptions();
    }
    // If siteurl is not set to autoload, check it specifically.     if ( ! isset( $alloptions['siteurl'] ) ) {
        $installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
    } else {
        $installed = $alloptions['siteurl'];
    }
    $wpdb->suppress_errors( $suppress );

    $installed = ! empty( $installed );
    wp_cache_set( 'is_blog_installed', $installed );

    if ( $installed ) {
        return true;
    }

    // If visiting repair.php, return true and let it take over.     if ( defined( 'WP_REPAIRING' ) ) {
        return true;
    }

    $suppress = $wpdb->suppress_errors();

    
$query .= ' GROUP BY post_status';

    $results = (array) $wpdb->get_results( $wpdb->prepare( $query$type ), ARRAY_A );
    $counts  = array_fill_keys( get_post_stati(), 0 );

    foreach ( $results as $row ) {
        $counts[ $row['post_status'] ] = $row['num_posts'];
    }

    $counts = (object) $counts;
    wp_cache_set( $cache_key$counts, 'counts' );

    /** * Filters the post counts by status for the current post type. * * @since 3.7.0 * * @param stdClass $counts An object containing the current post_type's post * counts by status. * @param string $type Post type. * @param string $perm The permission to determine if the posts are 'readable' * by the current user. */
// Total spam in queue     // get_option( 'akismet_spam_count' ) is the total caught ever     public static function get_spam_count( $type = false ) {
        global $wpdb;

        if ( !$type ) { // total             $count = wp_cache_get( 'akismet_spam_count', 'widget' );
            if ( false === $count ) {
                $count = wp_count_comments();
                $count = $count->spam;
                wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
            }
            return $count;
        } elseif ( 'comments' == $type || 'comment' == $type ) { // comments             $type = '';
        }

        return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type = %s", $type ) );
    }

    // Check connectivity between the WordPress blog and Akismet's servers.     // Returns an associative array of server IP addresses, where the key is the IP address, and value is true (available) or false (unable to connect).

    function wp_cache_set_multiple( array $data$group = '', $expire = 0 ) {
        $values = array();

        foreach ( $data as $key => $value ) {
            $values[ $key ] = wp_cache_set( $key$value$group$expire );
        }

        return $values;
    }
endif;

if ( ! function_exists( 'wp_cache_get_multiple' ) ) :
    /** * Retrieves multiple values from the cache in one call. * * Compat function to mimic wp_cache_get_multiple(). * * @ignore * @since 5.5.0 * * @see wp_cache_get_multiple() * * @param array $keys Array of keys under which the cache contents are stored. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param bool $force Optional. Whether to force an update of the local cache * from the persistent cache. Default false. * @return array Array of return values, grouped by key. Each value is either * the cache contents on success, or false on failure. */
if ( $result ) {
            $result = get_post( $result );
        }
        return $result;
    }

    $result = $wpdb->get_var( $query );
    if ( null === $result ) {
        $result = '';
    }

    wp_cache_set( $cache_key$result, 'post-queries' );

    if ( $result ) {
        $result = get_post( $result );
    }

    return $result;
}

/** * Retrieves the adjacent post relational link. * * Can either be next or previous post relational link. * * @since 2.8.0 * * @param string $title Optional. Link title format. Default '%title'. * @param bool $in_same_term Optional. Whether link should be in the same taxonomy term. * Default false. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * Default empty. * @param bool $previous Optional. Whether to display link to previous or next post. * Default true. * @param string $taxonomy Optional. Taxonomy, if `$in_same_term` is true. Default 'category'. * @return string|void The adjacent post relational link URL. */
'posts_per_page'         => 1,
                'no_found_rows'          => true,
                'cache_results'          => true,
                'update_post_meta_cache' => false,
                'update_post_term_cache' => false,
                'lazy_load_term_meta'    => false,
            )
        );
        if ( ! empty( $changeset_post_query->posts ) ) {
            // Note: 'fields'=>'ids' is not being used in order to cache the post object as it will be needed.             $changeset_post_id = $changeset_post_query->posts[0]->ID;
            wp_cache_set( $uuid$changeset_post_id$cache_group );
            return $changeset_post_id;
        }

        return null;
    }

    /** * Gets changeset posts. * * @since 4.9.0 * * @param array $args { * Args to pass into `get_posts()` to query changesets. * * @type int $posts_per_page Number of posts to return. Defaults to -1 (all posts). * @type int $author Post author. Defaults to current user. * @type string $post_status Status of changeset. Defaults to 'auto-draft'. * @type bool $exclude_restore_dismissed Whether to exclude changeset auto-drafts that have been dismissed. Defaults to true. * } * @return WP_Post[] Auto-draft changesets. */
$query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
    }

    $key          = md5( $query );
    $last_changed = wp_cache_get_last_changed( 'posts' );
    $cache_key    = "find_post_by_old_slug:$key:$last_changed";
    $cache        = wp_cache_get( $cache_key, 'post-queries' );
    if ( false !== $cache ) {
        $id = $cache;
    } else {
        $id = (int) $wpdb->get_var( $query );
        wp_cache_set( $cache_key$id, 'post-queries' );
    }

    return $id;
}

/** * Find the post ID for redirecting an old date. * * @since 4.9.3 * @access private * * @see wp_old_slug_redirect() * @global wpdb $wpdb WordPress database abstraction object. * * @param string $post_type The current post type based on the query vars. * @return int The Post ID. */
// Distinguish between `false` as a default, and not passing one.     $passed_default = func_num_args() > 1;

    if ( ! wp_installing() ) {
        // Prevent non-existent options from triggering multiple queries.         $notoptions = wp_cache_get( 'notoptions', 'options' );

        // Prevent non-existent `notoptions` key from triggering multiple key lookups.         if ( ! is_array( $notoptions ) ) {
            $notoptions = array();
            wp_cache_set( 'notoptions', $notoptions, 'options' );
        }

        if ( isset( $notoptions[ $option ] ) ) {
            /** * Filters the default value for an option. * * The dynamic portion of the hook name, `$option`, refers to the option name. * * @since 3.4.0 * @since 4.4.0 The `$option` parameter was added. * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value. * * @param mixed $default_value The default value to return if the option does not exist * in the database. * @param string $option Option name. * @param bool $passed_default Was `get_option()` passed a default value? */
$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
    $term_ids   = "'" . implode( "', '", $term_ids ) . "'";

    $sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order";

    $last_changed = wp_cache_get_last_changed( 'terms' );
    $cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
    $cache        = wp_cache_get( $cache_key, 'term-queries' );
    if ( false === $cache ) {
        $object_ids = $wpdb->get_col( $sql );
        wp_cache_set( $cache_key$object_ids, 'term-queries' );
    } else {
        $object_ids = (array) $cache;
    }

    if ( ! $object_ids ) {
        return array();
    }
    return $object_ids;
}

/** * Given a taxonomy query, generates SQL to be appended to a main query. * * @since 3.1.0 * * @see WP_Tax_Query * * @param array $tax_query A compact tax query * @param string $primary_table * @param string $primary_id_column * @return string[] */
$last_changed = wp_cache_get_last_changed( 'posts' );

    $limit = $parsed_args['limit'];

    if ( 'monthly' === $parsed_args['type'] ) {
        $query   = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
        $key     = md5( $query );
        $key     = "wp_get_archives:$key:$last_changed";
        $results = wp_cache_get( $key, 'post-queries' );
        if ( ! $results ) {
            $results = $wpdb->get_results( $query );
            wp_cache_set( $key$results, 'post-queries' );
        }
        if ( $results ) {
            $after = $parsed_args['after'];
            foreach ( (array) $results as $result ) {
                $url = get_month_link( $result->year, $result->month );
                if ( 'post' !== $parsed_args['post_type'] ) {
                    $url = add_query_arg( 'post_type', $parsed_args['post_type']$url );
                }
                /* translators: 1: Month name, 2: 4-digit year. */
                $text = sprintf( __( '%1$s %2$d' )$wp_locale->get_month( $result->month )$result->year );
                if ( $parsed_args['show_post_count'] ) {
                    
case 'blog':
            $comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
            break;
        case 'server':
            $add_seconds_server = gmdate( 'Z' );

            $comment_modified_date = $wpdb->get_var( $wpdb->prepare( "SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server ) );
            break;
    }

    if ( $comment_modified_date ) {
        wp_cache_set( $key$comment_modified_date, 'timeinfo' );

        return $comment_modified_date;
    }

    return false;
}

/** * Retrieves the total comment counts for the whole site or a single post. * * @since 2.0.0 * * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that * comment counts for the whole site will be retrieved. * @return int[] { * The number of comments keyed by their status. * * @type int $approved The number of approved comments. * @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending). * @type int $spam The number of spam comments. * @type int $trash The number of trashed comments. * @type int $post-trashed The number of comments for posts that are in the trash. * @type int $total_comments The total number of non-trashed comments, including spam. * @type int $all The total number of pending or approved comments. * } */

    $can_use_cached = ! wp_is_development_mode( 'theme' );

    $settings = false;
    if ( $can_use_cached ) {
        $settings = wp_cache_get( $cache_key$cache_group );
    }

    if ( false === $settings ) {
        $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
        if ( $can_use_cached ) {
            wp_cache_set( $cache_key$settings$cache_group );
        }
    }

    return _wp_array_get( $settings$path$settings );
}

/** * Gets the styles resulting of merging core, theme, and user data. * * @since 5.9.0 * @since 6.3.0 the internal link format "var:preset|color|secondary" is resolved * to "var(--wp--preset--font-size--small)" so consumers don't have to. * @since 6.3.0 `transforms` is now usable in the `context` parameter. In case [`transforms`]['resolve_variables'] * is defined, variables are resolved to their value in the styles. * * @param array $path Path to the specific style to retrieve. Optional. * If empty, will return all styles. * @param array $context { * Metadata to know where to retrieve the $path from. Optional. * * @type string $block_name Which block to retrieve the styles from. * If empty, it'll return the styles for the global context. * @type string $origin Which origin to take data from. * Valid values are 'all' (core, theme, and user) or 'base' (core and theme). * If empty or unknown, 'all' is used. * @type array $transforms Which transformation(s) to apply. * Valid value is array( 'resolve-variables' ). * If defined, variables are resolved to their value in the styles. * } * @return mixed The styles array or individual style value to retrieve. */
$this->posts      = array_map( 'intval', $this->posts );
            $this->post_count = count( $this->posts );
            $this->set_found_posts( $q$limits );

            if ( $q['cache_results'] && $id_query_is_cacheable ) {
                $cache_value = array(
                    'posts'         => $this->posts,
                    'found_posts'   => $this->found_posts,
                    'max_num_pages' => $this->max_num_pages,
                );

                wp_cache_set( $cache_key$cache_value, 'post-queries' );
            }

            return $this->posts;
        }

        if ( 'id=>parent' === $q['fields'] ) {
            if ( null === $this->posts ) {
                $this->posts = $wpdb->get_results( $this->request );
            }

            $this->post_count = count( $this->posts );
            
$user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
        if ( ! $user_count ) {
            $blog_users = new WP_User_Query(
                array(
                    'blog_id'     => $blog['blog_id'],
                    'fields'      => 'ID',
                    'number'      => 1,
                    'count_total' => true,
                )
            );
            $user_count = $blog_users->get_total();
            wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
        }

        printf(
            '<a href="%1$s">%2$s</a>',
            esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
            number_format_i18n( $user_count )
        );
    }

    /** * Handles the plugins column output. * * @since 4.3.0 * * @param array $blog Current site. */
$term_objects = $this->populate_terms( $cache );
                $cache        = $this->format_terms( $term_objects$_fields );
            }

            $this->terms = $cache;
            return $this->terms;
        }

        if ( 'count' === $_fields ) {
            $count = $wpdb->get_var( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared             wp_cache_set( $cache_key$count, 'term-queries' );
            return $count;
        }

        $terms = $wpdb->get_results( $this->request ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
        if ( empty( $terms ) ) {
            wp_cache_add( $cache_key, array(), 'term-queries' );
            return array();
        }

        $term_ids = wp_list_pluck( $terms, 'term_id' );
        
Home | Imprint | This part of the site doesn't use cookies.