Support » Themes and Templates » Reference a prior WP_Query

  • Resolved Jeal


    A site I’m building has a blog feed for a news category. Simple enough. But I also have a sidebar feed that pulls from that news category, therefore when the news page is loaded, the same items show up twice.*

    I would like to make it so my news feed page* takes the two most recent items, and from that point, the sidebar will use the next-most recent remaining 3 items, and only if the current page is news of course*.

    I tried this, knowing it proably wouldn’t work:

    //page-news.php which includes sidebar.php
    $latest_news = new WP_Query('cat="16"&posts_per_page="2"');
    //loop and feed for news, working just fine
    //then later... (sidebar.php)
    if( $latest_news->have_posts() ) : while( $latest_news->have_posts() ) : $latest_news->the_post()
    //nothing shows up here

    So this confirmed that $latest_news gets lost between the two php files. How would I reference that prior query AND remove the 2 posts that were already used?

    *EDITS for clarity

Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi


    Forum Moderator

    Why not use 2 queries and simply use the offset parameter to skip the first 2 posts in the second, sidebar, query?

    I wasn’t aware of an offset option :).

    I was also thinking the less queries the better, right? Seems like a waste resources to do the same thing twice. In my case, its a small site with little traffic, but still no reason to build inefficiently.

    That sounds like it will work though, thanks



    Forum Moderator

    There’s a whole list of parameters you can use at

    Happy querying. 🙂

    Turns out this worked great, but had a pretty serious bug.

    The sidebar is on (almost) all the pages, so when you’re viewing an individual post the first two latest news items don’t show up, ever.

    so in my sidebar .php, right before the news category loop, I needed a way to offset manually. I started by getting the post_id before the loop even started:

    wp_reset_query(); //reset
    $current_post = $post->ID;

    Then within the loop:

    //if, while, the...
    <?php $current_query = $post->ID; ?>
    <?php if (  $current_post == $current_query ){
    continue; //skip the post, display nothing
    } ?>
    //the post name & link
    <?php endwhile; endif ?>

    The latest news checks if the current post is part of the query and skips it if it does.

    This results in one less post than I want, so I added code to add a certain amount to post_per_page if the current page is a news item.

    Additionally, I needed to add code to modify the offset if I’m on a news blog feed page, that defaults to null.

    $post_per_page = 5; //default count
    if ( is_category( 16 ) ) //if news category
    $posts_per_page = 6; //display 6 posts because one will be skipped
    $offset = null; //default offset
    if ( is_page( '123' ) ) //is this the news page?
    $offset = 2;
    $args = array(
    'posts_per_page' => $post_per_page,
    'offset' => $offset
    ... //more conditions
    $latest_news_sidebar = new WP_Query( $args );( 
    //the loop

    Thanks again for the help. I just thought I’d write that up for my own good and anyone who stumbles onto this page.

    Edit: Still having some issues… :\

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Reference a prior WP_Query’ is closed to new replies.