Support » Fixing WordPress » Multiple Posts — Restoring first query

  • Resolved valuxes


    I searched the codex and existing posts. This one in particular seemed to be what I am looking for:

    1. Save the original query in a temp_query var
    <?php $temp_query = clone $wp_query; ?>

    2. Do a new WP_query to extract what I want and display these posts (just the titles)
    <?php $my_query = new WP_Query(array(
    ‘order’=>’DESC’,)); ?>

    3. Restore the original query so the comments associated with the original post can display
    <?php $wp_query = clone $temp_query; ?>

    However, I keep getting the comments of the last post in the new WP_query (in item 2)

    Here is a link to my page:

    There should be no comment associated with the post currently displayed. The 5 comments come from the last post from the table just before the comments.

    Any help is appreciated. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Please paste all the code from the theme template file (probably index.php) that is displaying those posts into a pastebin such as, and report the link back here. Maybe someone can spot your problem. Thanks.

    Basically, I display a post, then do the above code to fetch all posts that match a couple of custom fields and display these in a table.

    The Comments should apply to the original post displayed, but it is currently always referencing the last post in the table.

    I have a feeling it has nothing to do with saving/restoring $wp_query but rather that all the comments from all matching posts are retrieved when I do the query to populate the table. Maybe I need to re-retrieve the comments after restoring $wp_query… how 🙁

    Thanks for any help!

    I tried:

    $wp_comments = null;

    but still same result…

    Not too sure why you even need line 1 or lines 55 and 56.

    Maybe this at the end there will resolve your problem:

    wp_reset_query();  // Restore global post data stomped by the_post().


    It works! You rock!

    Wanting to learn more about wp_reset_query(), I googled it and found this link:

    Thanks again! Need any photo/camera help, drop me a line.


Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Multiple Posts — Restoring first query’ is closed to new replies.