Make WordPress Core

Changeset 52975


Ignore:
Timestamp:
03/22/2022 09:56:59 AM (3 years ago)
Author:
spacedmonkey
Message:

Menus: Improve cache priming in the wp_get_nav_menu_items function.

In wp_get_nav_menu_items multiple function calls to get_terms and get_posts were being used to load post/term objects into
memory. This change replaces calls to get_terms and get_posts with calls to _prime_post_caches and 
_prime_term_caches. These functions are designed to prime object caches and do not have the overhead of a query object. This
change also replaces multiple queries with a single query, saving many SQL queries per page load.

Props Spacedmonkey, peterwilsoncc.
Fixes #55428.

--This line, and those below,

will be ignored--

M src/wp-includes/nav-menu.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r52332 r52975  
    717717    }
    718718
    719     // Get all posts and terms at once to prime the caches.
    720     if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) {
     719    // Prime posts and terms caches.
     720    if ( empty( $fetched[ $menu->term_id ] ) ) {
    721721        $fetched[ $menu->term_id ] = true;
    722         $posts                     = array();
    723         $terms                     = array();
     722        $post_ids                  = array();
     723        $term_ids                  = array();
    724724        foreach ( $items as $item ) {
    725725            $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
    726             $object    = get_post_meta( $item->ID, '_menu_item_object', true );
    727726            $type      = get_post_meta( $item->ID, '_menu_item_type', true );
    728727
    729728            if ( 'post_type' === $type ) {
    730                 $posts[ $object ][] = $object_id;
     729                $post_ids[] = (int) $object_id;
    731730            } elseif ( 'taxonomy' === $type ) {
    732                 $terms[ $object ][] = $object_id;
     731                $term_ids[] = (int) $object_id;
    733732            }
    734733        }
    735734
    736         if ( ! empty( $posts ) ) {
    737             foreach ( array_keys( $posts ) as $post_type ) {
    738                 get_posts(
    739                     array(
    740                         'post__in'               => $posts[ $post_type ],
    741                         'post_type'              => $post_type,
    742                         'nopaging'               => true,
    743                         'update_post_term_cache' => false,
    744                     )
    745                 );
    746             }
    747         }
    748         unset( $posts );
    749 
    750         if ( ! empty( $terms ) ) {
    751             foreach ( array_keys( $terms ) as $taxonomy ) {
    752                 get_terms(
    753                     array(
    754                         'taxonomy'     => $taxonomy,
    755                         'include'      => $terms[ $taxonomy ],
    756                         'hierarchical' => false,
    757                     )
    758                 );
    759             }
    760         }
    761         unset( $terms );
     735        if ( ! empty( $post_ids ) ) {
     736            _prime_post_caches( $post_ids, false );
     737        }
     738        unset( $post_ids );
     739
     740        if ( ! empty( $term_ids ) ) {
     741            _prime_term_caches( $term_ids );
     742        }
     743        unset( $term_ids );
    762744    }
    763745
Note: See TracChangeset for help on using the changeset viewer.