Make WordPress Core


Ignore:
Timestamp:
06/22/2017 03:18:18 AM (8 years ago)
Author:
boonebgorges
Message:

Cache results in get_objects_in_term().

This helps to reduce database queries when generating nav menus.

Props spacedmonkey.
Fixes #37094.

File:
1 edited

Legend:

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

    r40920 r40921  
    651651    $term_ids = "'" . implode( "', '", $term_ids ) . "'";
    652652
    653     $object_ids = $wpdb->get_col("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");
     653    $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";
     654
     655    $last_changed = wp_cache_get_last_changed( 'terms' );
     656    $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
     657    $cache = wp_cache_get( $cache_key, 'terms' );
     658    if ( false === $cache ) {
     659        $object_ids = $wpdb->get_col( $sql );
     660        wp_cache_set( $cache_key, $object_ids, 'terms' );
     661    } else {
     662        $object_ids = (array) $cache;
     663    }
    654664
    655665    if ( ! $object_ids ){
Note: See TracChangeset for help on using the changeset viewer.