0

Besides the main loop in index.php, I'm calling several other custom queries across the theme, like this:

$new_query = new WP_Query(array(
    // some args
));

if ($new_query->have_posts()) : 

    while ( $new_query->have_posts() ) : $new_query->the_post();

        // This is the same template that I use in the main query or other custom queries
        get_template_part( 'template-parts/content' );

    endwhile; 

endif;
wp_reset_postdata();

Now I need to check from within the template file if it's being called from this custom query or from the main query.

So in content.php

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

        <?php if ( is_main_query() ) {
            // Do some staff
        } else {
            // Do other staff

        } ?>

</article>

is_main_query() is always returning true, since it's referring to global $wp_query, I suppose. I could use $new_query->is_main_query() but it's not possible from within the template file. So how this could be solved? Thanks!

EDIT:

I found one solution here Which custom query am I in and how can I access its properties & methods? The point is to set global boolean and check it in the template file

$new_query = new WP_Query(array(
    // some args
));

$GLOBALS['is_custom_query'] = FALSE;

if ($new_query->have_posts()) : 

    while ( $new_query->have_posts() ) : $new_query->the_post();

        $GLOBALS['is_custom_query'] = TRUE;
        get_template_part( 'template-parts/content' );
        $GLOBALS['is_custom_query'] = FALSE;

    endwhile; 

endif;
wp_reset_postdata();

Not sure how reliable this solution is.

2

Use in_the_loop(), it returns true if we are in the main loop, false if we are not, like on a custom query loop

Return: (bool) True if caller is within loop, false if loop hasn't started or ended.

The difference between is_main_query() and in_the_loop() is that is_main_query() uses WP_Query::is_main_query() which in turn uses the global $wp_the_query. $wp_is_query stores the main query, that is why is_main_query() always returns true inside the loop. You should also read this post

So, in short, you can do

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <?php if ( in_the_loop() ) {
        // Do some staff
    } else {
        // Do other staff

    } ?>

</article>
  • Thanks Pieter. I actually tried in_the_loop(), but it returns true every time. Confusing. – Nikita Sep 30 '15 at 7:44
  • Then there is some other issue on your page. It is working perfectly on my test install – Pieter Goosen Sep 30 '15 at 7:50
  • I actually tried your code and only replaced is_main_query() with in_the_loop(). Try deactivating plugins, clear caches and switch to a bundled theme and retest – Pieter Goosen Sep 30 '15 at 7:52
  • Doh, what a shame. Indeed, the mistake was on my part. I inserted this custom query right before endwhile of the main loop. Your answer is correct, thanks! – Nikita Sep 30 '15 at 9:00
  • No problem, glad you sorted it ;-). Enjoy – Pieter Goosen Sep 30 '15 at 9:02

Your Answer

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

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