0

I need to run three queries on the same page. The 'core' query is the same all three times - selecting posts in a particular taxonomy. I then need to run three queries on that subset looking for particular terms in a different taxonomy.

Currently I run three queries of core+a, core+b and core+c, using reset_post_data() after each. This works fine, but the posts table that the core query is of is relatively small at the moment. When I've finished inputting all of the data, plus data for another 'core' query, it's probably going to multiply in size tenfold.

Is there a way of (should I be) running first the core query, then +a, +b and +c queries on that? If so, how do I retain and invoke the core query? By using rewind_posts() after each sub query?

i.e. core query -> queryA -> rewind_posts() -> queryB -> rewind_posts() - queryC -> reset_post_data()

Or would it make so little difference to the overhead that I'm worrying unnecessarily?

At the moment my +a etc. queries are just checking if any of the posts in the core query HAVE the terms but I can probably render each page much more efficiently (quickly!) if I can query just those posts identified by the core query.

I've tried to work this out for myself from the WordPress Codex etc., but I guess I'm missing something, so am hoping someone can point me in the right direction. (Or at least tell me if I'm wasting my time so I can stop looking and move on to the next challenge!)

The code I'm currently using, which works, is:

        $args = array( // Cuttings
        'post_type' => 'nqsnip',
        'tax_query' => array(
            array(
                'taxonomy' => 'nqtos',
                'field'    => 'slug',
                'terms'    => '1',
            ),
            array(
                'taxonomy' => 'nqshow',
                'field'    => 'slug',
                'terms'    => $post->post_name,
            ),
        ),
    );
    $cutting_query = new WP_Query($args);
    if($cutting_query->found_posts > '2') {
        $pic_c = '';
    }
    elseif($cutting_query->found_posts > '0') {
        $pic_c = '<img src="/wp-content/uploads/site-pix/cutting-grey.gif" style="float: right; margin-left: 8px; width: 20px;" title="Cuttings" alt="Cutting icon">';
    }
    else {
        $pic_c = '<img src="/wp-content/uploads/site-pix/cutting.gif" style="float: right; margin-left: 8px; width: 20px;" title="Cuttings" alt="Cutting icon">';
    }       
    wp_reset_postdata();

    $args = array( // Questions
        'post_type' => 'nqsnip',
        'tax_query' => array(
            array(
                'taxonomy' => 'nqtos',
                'field'    => 'slug',
                'terms'    => '2',
            ),
            array(
                'taxonomy' => 'nqshow',
                'field'    => 'slug',
                'terms'    => $post->post_name,
            ),
        ),
    );
    $question_query = new WP_Query($args);
    if($question_query->found_posts > '0') {
        $pic_q = '';
    }
    else {
        $pic_q = '<img src="/wp-content/uploads/site-pix/question.gif" style="float: right; margin-left: 8px; width: 20px;" title="Questions" alt="Question icon">';
    }   
    wp_reset_postdata();

    $args = array( // Remarks 
        'post_type' => 'nqsnip',
        'tax_query' => array(
            array(
                'taxonomy' => 'nqtos',
                'field'    => 'slug',
                'terms'    => '3',
            ),
            array(
                'taxonomy' => 'nqshow',
                'field'    => 'slug',
                'terms'    => $post->post_name,
            ),
        ),
    );
    $remark_query = new WP_Query($args);
    if($remark_query->found_posts > '0') {
        $pic_r = '';
    }
    else {
        $pic_r = '<img src="/wp-content/uploads/site-pix/comment.gif" style="float: right; margin-left: 8px; width: 20px;" title="Questions" alt="Question icon">';
    }   
    wp_reset_postdata();

    $excerpt = $pic_c . $pic_q . $pic_r;

As you can see, the first and third elements of each query are the same, only the second element in each query differs. And the second element only needs to be performed on those posts which would be identified by the first and third elements.

  • Post your code please. – s_ha_dum Jun 9 '15 at 14:57
  • @s_ha_dum: Added. There's quite a lot, but it's 'the whole package' that I'm trying to find a more efficient way of doing. – Kevin4fm Jun 9 '15 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.