1

I have a main query, in which I set up a new custom query to retrieve posts (think about 'related posts' or something like that). Than in each of those posts, I'm setting an other custom query, load data from a single post, then I should find a way to reset postdata to the first custom query. How can I do that? I tried wp_reset_postdata(), but that resets to main query.

A simplified code would look thus:

while ( have_posts() ) : the_post();

    //Set up custom query
    $args = array(
        //Query args
    );
    $custom_query = new WP_Query( $args );

    //Custom query loop:
    while ( $custom_query->have_posts() ) : $custom_query->the_post();
        //Display some data,

        //then set up a custom query again
        $args = array(
            //Different query args
        );
        $custom_nested_query = new WP_Query( $args );

        //Nested custom query loop
        while ( $custom_nested_query->have_posts() ) : $custom_nested_query->the_post();
            //Do stuff, then reset to $custom_query since
            //I still need the post before $custom_query proceeds with the loop and loads the next one
        endwhile;

        //Display some more data from post queried by $custom_query before $nested_query was created
    endwhile;
endwhile;

1 Answer 1

1

You can save the $post in some temp variable and just set the $post back to the temp variable

global $post;
while ( have_posts() ) : the_post();

    //Set up custom query
    $args = array(
        //Query args
    );
    $custom_query = new WP_Query( $args );

    //Custom query loop:
    while ( $custom_query->have_posts() ) : $custom_query->the_post();
        // Save global post into temp variable
        $temp_post = $post;

        //Display some data,

        //then set up a custom query again
        $args = array(
            //Different query args
        );
        $custom_nested_query = new WP_Query( $args );

        //Nested custom query loop
        while ( $custom_nested_query->have_posts() ) : $custom_nested_query->the_post();
            //Do stuff, then reset to $custom_query since
            //I still need the post before $custom_query proceeds with the loop and loads the next one
        endwhile;

        // Set the global $post back to the first custom query
        $post = $temp_post;
        //Display some more data from post queried by $custom_query before $nested_query was created

    endwhile;
    wp_reset_postdata();
endwhile;

Or just use foreach($custom_nested_query->posts as $mypost).

2
  • Thanks, I'm going to try this. As far as I remember I've already did something like this, but I may have not called global $post; in the beginning. Is it possible, that it was the reason it didn't work for me?
    – galingong
    Commented Dec 20, 2017 at 22:42
  • No. I called him for anycase but the $post already defined from what I see.
    – Shibi
    Commented Dec 20, 2017 at 22:52

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.