Menus: Convert `wp_get_nav_menu_items` function to use a taxonomy query.

Change the `wp_get_nav_menu_items` function to replace the call to `get_objects_in_term` with a simple taxonomy query. This change improves performance as it results in one fewer query to retrieve menu items from the 
database. 

Props Spacedmonkey, peterwilsoncc.
Fixes #55372.




Built from https://develop.svn.wordpress.org/trunk@53010


git-svn-id: http://core.svn.wordpress.org/trunk@52599 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2022-03-29 11:07:11 +00:00
parent 1de43a471c
commit c220f23155
2 changed files with 12 additions and 8 deletions

View File

@ -693,12 +693,11 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
static $fetched = array(); static $fetched = array();
$items = get_objects_in_term( $menu->term_id, 'nav_menu' ); if ( ! taxonomy_exists( 'nav_menu' ) ) {
if ( is_wp_error( $items ) ) {
return false; return false;
} }
$defaults = array( $defaults = array(
'order' => 'ASC', 'order' => 'ASC',
'orderby' => 'menu_order', 'orderby' => 'menu_order',
'post_type' => 'nav_menu_item', 'post_type' => 'nav_menu_item',
@ -706,11 +705,16 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
'output' => ARRAY_A, 'output' => ARRAY_A,
'output_key' => 'menu_order', 'output_key' => 'menu_order',
'nopaging' => true, 'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'nav_menu',
'field' => 'term_taxonomy_id',
'terms' => $menu->term_taxonomy_id,
),
),
); );
$args = wp_parse_args( $args, $defaults ); $args = wp_parse_args( $args, $defaults );
$args['include'] = $items; if ( $menu->count > 0 ) {
if ( ! empty( $items ) ) {
$items = get_posts( $args ); $items = get_posts( $args );
} else { } else {
$items = array(); $items = array();

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.0-alpha-53007'; $wp_version = '6.0-alpha-53010';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.