I'm filtering the_content in order to append a grid of related posts onto a single post with a nested WP_Query and would like to use the same content.php file as I do for my main query. The problem is that my 'normal' conditionals no longer work as required, for example is_single() returns true, (as expected, we're on a single) but I really need to treat this instance of the loop as !is_single().

I've attempted to differentiate using is_main_query() but that's also returning true inside my nested query.

My question is, how can I differentiate between my 'main' and my 'nested' query in order to conditionally output the content?

1 Answer 1


You could check if the current post ID is the same as the 'queried object' ID, which is whichever post the current URL represents, and is separate from the current post within the loop. If both those IDs are the same then you can assume that the current content being filtered is for the content of the main post, and not a nested query for a different post.

function wpse_308358_content_filter( $content ) {
    if ( is_single() && get_queried_object_id() === get_the_ID() ) {
        $content .= 'Appended content.';

    return $content;
add_filter( 'the_content', 'wpse_308358_content_filter' );

is_single() adds protection against the possibility that you're displaying the full content on category archives and you have a category that happens to have the same ID as a post.

  • There’s something wrong with that code... Take a look at that if statement ;) Jul 12, 2018 at 19:54

Your Answer

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

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