WP_Query Performance Issues with meta_query

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.

Read More

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.

Related posts

Leave a Reply

1 comment

  1. 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.