wp_cache_add example

$cache_value = wp_cache_get( $cache_key, 'comment-queries' );
        if ( false === $cache_value ) {
            $comment_ids = $this->get_comment_ids();
            if ( $comment_ids ) {
                $this->set_found_comments();
            }

            $cache_value = array(
                'comment_ids'    => $comment_ids,
                'found_comments' => $this->found_comments,
            );
            wp_cache_add( $cache_key$cache_value, 'comment-queries' );
        } else {
            $comment_ids          = $cache_value['comment_ids'];
            $this->found_comments = $cache_value['found_comments'];
        }

        if ( $this->found_comments && $this->query_vars['number'] ) {
            $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
        }

        // If querying for a count only, there's nothing more to do.         if ( $this->query_vars['count'] ) {
            


        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' );
        _prime_term_caches( $term_ids, false );
        $term_objects = $this->populate_terms( $terms );

        if ( $child_of ) {
            foreach ( $taxonomies as $_tax ) {
                $children = _get_term_hierarchy( $_tax );
                if ( ! empty( $children ) ) {
                    
$taxonomy = get_taxonomy( $taxonomy_name );

        if ( ! $taxonomy->show_in_quick_edit ) {
            continue;
        }

        if ( $taxonomy->hierarchical ) {

            $terms = get_object_term_cache( $post->ID, $taxonomy_name );
            if ( false === $terms ) {
                $terms = wp_get_object_terms( $post->ID, $taxonomy_name );
                wp_cache_add( $post->ID, wp_list_pluck( $terms, 'term_id' )$taxonomy_name . '_relationships' );
            }
            $term_ids = empty( $terms ) ? array() : wp_list_pluck( $terms, 'term_id' );

            echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">' . implode( ',', $term_ids ) . '</div>';

        } else {

            $terms_to_edit = get_terms_to_edit( $post->ID, $taxonomy_name );
            if ( ! is_string( $terms_to_edit ) ) {
                $terms_to_edit = '';
            }

            
if ( false === $cache_value ) {
            $network_ids = $this->get_network_ids();
            if ( $network_ids ) {
                $this->set_found_networks();
            }

            $cache_value = array(
                'network_ids'    => $network_ids,
                'found_networks' => $this->found_networks,
            );
            wp_cache_add( $cache_key$cache_value, 'network-queries' );
        } else {
            $network_ids          = $cache_value['network_ids'];
            $this->found_networks = $cache_value['found_networks'];
        }

        if ( $this->found_networks && $this->query_vars['number'] ) {
            $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
        }

        // If querying for a count only, there's nothing more to do.         if ( $this->query_vars['count'] ) {
            
$climits  = ( ! empty( $climits ) ) ? $climits : '';

            $comments_request = "SELECT $distinct {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";

            $key          = md5( $comments_request );
            $last_changed = wp_cache_get_last_changed( 'comment' ) . ':' . wp_cache_get_last_changed( 'posts' );

            $cache_key   = "comment_feed:$key:$last_changed";
            $comment_ids = wp_cache_get( $cache_key, 'comment-queries' );
            if ( false === $comment_ids ) {
                $comment_ids = $wpdb->get_col( $comments_request );
                wp_cache_add( $cache_key$comment_ids, 'comment-queries' );
            }
            _prime_comment_caches( $comment_ids );

            // Convert to WP_Comment.             /** @var WP_Comment[] */
            $this->comments      = array_map( 'get_comment', $comment_ids );
            $this->comment_count = count( $this->comments );

            $post_ids = array();

            foreach ( $this->comments as $comment ) {
                

function get_terms_to_edit( $post_id$taxonomy = 'post_tag' ) {
    $post_id = (int) $post_id;
    if ( ! $post_id ) {
        return false;
    }

    $terms = get_object_term_cache( $post_id$taxonomy );
    if ( false === $terms ) {
        $terms = wp_get_object_terms( $post_id$taxonomy );
        wp_cache_add( $post_idwp_list_pluck( $terms, 'term_id' )$taxonomy . '_relationships' );
    }

    if ( ! $terms ) {
        return false;
    }
    if ( is_wp_error( $terms ) ) {
        return $terms;
    }
    $term_names = array();
    foreach ( $terms as $term ) {
        $term_names[] = $term->name;
    }


        $_network = wp_cache_get( $network_id, 'networks' );

        if ( false === $_network ) {
            $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );

            if ( empty( $_network ) || is_wp_error( $_network ) ) {
                $_network = -1;
            }

            wp_cache_add( $network_id$_network, 'networks' );
        }

        if ( is_numeric( $_network ) ) {
            return false;
        }

        return new WP_Network( $_network );
    }

    /** * Creates a new WP_Network object. * * Will populate object properties from the object provided and assign other * default properties based on that information. * * @since 4.4.0 * * @param WP_Network|object $network A network object. */
/* * A "subdomain" installation can be re-interpreted to mean "can support any domain". * If we're not dealing with one of these installations, then the important part is determining * the network first, because we need the network's path to identify any sites. */
        $current_site = wp_cache_get( 'current_network', 'site-options' );
        if ( ! $current_site ) {
            // Are there even two networks installed?             $networks = get_networks( array( 'number' => 2 ) );
            if ( count( $networks ) === 1 ) {
                $current_site = array_shift( $networks );
                wp_cache_add( 'current_network', $current_site, 'site-options' );
            } elseif ( empty( $networks ) ) {
                // A network not found hook should fire here.                 return false;
            }
        }

        if ( empty( $current_site ) ) {
            $current_site = WP_Network::get_by_path( $domain$path, 1 );
        }

        if ( empty( $current_site ) ) {
            


        $_site = wp_cache_get( $site_id, 'sites' );

        if ( false === $_site ) {
            $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );

            if ( empty( $_site ) || is_wp_error( $_site ) ) {
                $_site = -1;
            }

            wp_cache_add( $site_id$_site, 'sites' );
        }

        if ( is_numeric( $_site ) ) {
            return false;
        }

        return new WP_Site( $_site );
    }

    /** * Creates a new WP_Site object. * * Will populate object properties from the object provided and assign other * default properties based on that information. * * @since 4.5.0 * * @param WP_Site|object $site A site object. */
$_post = wp_cache_get( $post_id, 'posts' );

        if ( ! $_post ) {
            $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );

            if ( ! $_post ) {
                return false;
            }

            $_post = sanitize_post( $_post, 'raw' );
            wp_cache_add( $_post->ID, $_post, 'posts' );
        } elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) {
            $_post = sanitize_post( $_post, 'raw' );
        }

        return new WP_Post( $_post );
    }

    /** * Constructor. * * @since 3.5.0 * * @param WP_Post|object $post Post object. */

function get_bookmark( $bookmark$output = OBJECT, $filter = 'raw' ) {
    global $wpdb;

    if ( empty( $bookmark ) ) {
        if ( isset( $GLOBALS['link'] ) ) {
            $_bookmark = & $GLOBALS['link'];
        } else {
            $_bookmark = null;
        }
    } elseif ( is_object( $bookmark ) ) {
        wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
        $_bookmark = $bookmark;
    } else {
        if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) {
            $_bookmark = & $GLOBALS['link'];
        } else {
            $_bookmark = wp_cache_get( $bookmark, 'bookmark' );
            if ( ! $_bookmark ) {
                $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );
                if ( $_bookmark ) {
                    $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
                    wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
                }


            // Don't return terms from invalid taxonomies.             if ( ! taxonomy_exists( $_term->taxonomy ) ) {
                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
            }

            $_term = sanitize_term( $_term$_term->taxonomy, 'raw' );

            // Don't cache terms that are shared between taxonomies.             if ( 1 === count( $terms ) ) {
                wp_cache_add( $term_id$_term, 'terms' );
            }
        }

        $term_obj = new WP_Term( $_term );
        $term_obj->filter( $term_obj->filter );

        return $term_obj;
    }

    /** * Constructor. * * @since 4.4.0 * * @param WP_Term|object $term Term object. */
if ( false === $cache_value ) {
            $site_ids = $this->get_site_ids();
            if ( $site_ids ) {
                $this->set_found_sites();
            }

            $cache_value = array(
                'site_ids'    => $site_ids,
                'found_sites' => $this->found_sites,
            );
            wp_cache_add( $cache_key$cache_value, 'site-queries' );
        } else {
            $site_ids          = $cache_value['site_ids'];
            $this->found_sites = $cache_value['found_sites'];
        }

        if ( $this->found_sites && $this->query_vars['number'] ) {
            $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
        }

        // If querying for a count only, there's nothing more to do.         if ( $this->query_vars['count'] ) {
            

function get_all_page_ids() {
    global $wpdb;

    $page_ids = wp_cache_get( 'all_page_ids', 'posts' );
    if ( ! is_array( $page_ids ) ) {
        $page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" );
        wp_cache_add( 'all_page_ids', $page_ids, 'posts' );
    }

    return $page_ids;
}

/** * Retrieves page data given a page ID or page object. * * Use get_post() instead of get_page(). * * @since 1.5.1 * @deprecated 3.5.0 Use get_post() * * @param int|WP_Post $page Page object or page ID. Passed by reference. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which * correspond to a WP_Post object, an associative array, or a numeric array, * respectively. Default OBJECT. * @param string $filter Optional. How the return value should be filtered. Accepts 'raw', * 'edit', 'db', 'display'. Default 'raw'. * @return WP_Post|array|null WP_Post or array on success, null on failure. */

function update_user_caches( $user ) {
    if ( $user instanceof WP_User ) {
        if ( ! $user->exists() ) {
            return false;
        }

        $user = $user->data;
    }

    wp_cache_add( $user->ID, $user, 'users' );
    wp_cache_add( $user->user_login, $user->ID, 'userlogins' );
    wp_cache_add( $user->user_nicename, $user->ID, 'userslugs' );

    if ( ! empty( $user->user_email ) ) {
        wp_cache_add( $user->user_email, $user->ID, 'useremail' );
    }
}

/** * Cleans all user caches. * * @since 3.0.0 * @since 4.4.0 'clean_user_cache' action was added. * @since 6.2.0 User metadata caches are now cleared. * * @param WP_User|int $user User object or ID to be cleaned from the cache */
Home | Imprint | This part of the site doesn't use cookies.