2

I'm working on a custom template for a new theme that uses a WP_Query instance to select posts from 2 post types with 2 custom fields that are NOT empty. Depending on the section of the site, a $current_zone variable may be set to determine the category to query from.

// Custom loop
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_loop_args = array(
    'post_type' => array('videos', 'post'),
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'paged' => $paged,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'article_source',
            'compare' => '!=',
            'value' => ''
        ),
        array(
            'key' => 'article_link',
            'compare' => '!=',
            'value' => ''
        )
    )
);

// Check the current zone
global $current_zone;

// Check for category match to current zone
if ( term_exists($current_zone, 'category') ) {
    $term = get_term_by('name', $current_zone, 'category');
    $custom_loop_args['cat'] = $term->term_id;
}

// Create unique identifier for caching
$cache_id = ( isset($term) ) ? '_term-' . $term->term_id : '_main';

// Run query or get transient cache
if ( ( $custom_loop = get_transient( "curated_wpquery$cache_id" ) ) === false ) {

    // It wasn't there, so regenerate the data and save the transient
    $custom_loop = new WP_Query( $custom_loop_args );
    set_transient( "curated_wpquery$cache_id", $custom_loop, ( 60 * 60 * 1 ) );
}

I'm using the transients in an effort to speed this up, which does work, but the initial query takes 35 seconds and a crazy amount of memory to complete. We have a pretty large database, with about 10,553 records in the posts table. I've cleared out revisions, but haven't noticed a performance increase.

So my real question is, how can I improve the performance of this query? Would a custom $wpdb query be best, or is querying custom fields in this way unproductive altogether?

Any help is much appreciated.

1

1 Answer 1

5

Custom field data is stored in the postmeta table, and it's likely that scanning through this table is causing slowness.

You should:

  • install the debug bar plugin which will give you more insight into the queries that are being executed against the database - you'll be able to see how long each query takes, and the actual SQL code that's being executed.
  • Find the queries that are taking a long time to run, and EXPLAIN them. This will give you a much better idea of what's going on, and how to proceed.
1
  • Thanks Anu, I've found that the OR relation operator was the culprit. The query went from 35.76 seconds to 2.18 just by switching that to AND. Oct 10, 2012 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.