Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#31724 closed defect (bug) (fixed)

Multiple calls get_terms() when: custom taxonomy (with no posts in it) and wp_nav_menu

Reported by: kg69design's profile kg69design Owned by: boonebgorges's profile boonebgorges
Milestone: 4.3 Priority: normal
Severity: normal Version: 4.1.1
Component: General Keywords: has-patch
Focuses: performance Cc:


  1. When custom taxonomy is registered but with no custom-posts assigned to it, and when there is a wp_nav_menu() in current theme - database queries increased significantly (from 30 to 100+) (detect with get_num_queries() and query-monitor-plugin)
  1. Problem disappeared when I:

--- delete wp_nav_menu() in current theme or
--- delete register_taxonomy() or
--- add 1+ custom-post into this taxonomy or
--- commented line 588 in /wp-includes/nav-menu.php (get_terms($taxonomy, array('include' => $terms[$taxonomy]) );)

Attachments (2)

menu.png (63.3 KB) - added by kg69design 9 years ago.
31724.diff (538 bytes) - added by boonebgorges 9 years ago.

Download all attachments as: .zip

Change History (8)

#1 @boonebgorges
9 years ago

  • Keywords reporter-feedback added

Hi kg69design - I'm not able to reproduce this. It's possible that you'd see an increase in database queries if get_terms() were not properly caching the results of a query that returns no terms. But it's not clear to me how such a call would be taking place inside of wp_get_nav_menu_items(). Do you have some custom code in place that adds taxonomy archives to your nav menu? Are you able to reproduce this problem on a WP installation with no plugins or custom themes (except for the register_taxonomy() code in question)?

9 years ago

#2 @kg69design
9 years ago

  • Keywords reporter-feedback removed

Thank you for your attention. My actions step by step.

  1. Install clean wordpress 4.1.1. with default theme "Twentyfifteen"
  2. Add get_num_queries() in footer.php - and get 22 queries on homepage
  3. Add register_post_type() and register_taxonomy() to function.php like this: (after that still get 22 queries on homepage)
add_action('init', 'custom_post_type_and_taxonomy'); 

function custom_post_type_and_taxonomy() {
	$args = array(  
		'label'			=> 'Equipment',
		'public'		=> true,
		'menu_position'		=> 5,
		'supports'		=> array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments'),
		'taxonomies'		=> array('catalog'),
		'rewrite'		=> array('slug' => 'equipment')
	register_post_type('equipment', $args); 

	$args = array(  
		'label'			=> 'Catalog',
		'show_admin_column'	=> true,
		'hierarchical'		=> true,
		'rewrite'		=> array('slug' => 'catalog')
	register_taxonomy('catalog', array('equipment'), $args);
  1. Create new taxonomy category "Test" and 10 subcategories in it (after that still get 22 queries on homepage)
  2. Create new empty menu in admin, assign it to location "Primary Menu" (after that get 21 queries on homepage)
  3. Add link to taxonomy category "Test" in this new menu (add screenshot "menu.png")
  4. Now I have 34 queries on homepage. If I add +1 subcategory to taxonomy category "Test" (another 1st-level taxonomy category does not increase queries on site) - I have +1 query on homepage. So, it seems that we have problem here.
  5. If I add one post in taxonomy category "Test" - I get 24 queries on homepage, that is seems to be normal. After that increasing sub-categories does not increase queries on site.

#3 @boonebgorges
9 years ago

  • Keywords has-patch 4.3-early added
  • Milestone changed from Awaiting Review to Future Release

Thanks for the additional info. The bit about child terms is critical to demonstrating the bug, and that wasn't mentioned the first time around.

When a taxonomy term is included in a nav menu, get_terms() is called in wp_get_nav_menu_items() to prime the term cache for that term. If that taxonomy item happens to have children, and does not have any posts, then get_terms() will manually descend the term hierarchy to find 0-count terms.,1941#L1938 This is important when you're trying to fetch a branch of the hierarchy tree, but it's not important in the case of nav menus. In fact, we shouldn't be traversing the tree at all when priming the nav menu cache - we only need to prime the cache for terms that are actually included in the nav menu.

See [31724.diff]. Could you test this to make sure it's eliminating the additional queries for you? Let's do this first thing for 4.3.

9 years ago

#4 @kg69design
9 years ago

Thank you for your attention again.
It seems that all is fine now. There are no additional queries more.

#5 @boonebgorges
9 years ago

  • Keywords 4.3-early removed
  • Milestone changed from Future Release to 4.3

#6 @boonebgorges
9 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 32294:

When priming the cache for taxonomy term nav items, don't fetch term descendants.

Descending the term tree causes unnecessary database queries when priming the
cache for a term with many descendants.

Fixes #31724.

Note: See TracTickets for help on using tickets.