WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] using post__not_in and pagination issue (7 posts)

  1. TimTee
    Member
    Posted 1 year ago #

    I am using a page template for the front page, and have 2 queries. First one retrieves the latest post from a category called 'todays post' using WP Query as the featured top post on the front page, and the second one is the main loop showing all posts. Both queries are pulling from 3 custom post types, I'm using the wp_pagenavi plugin. I have the main loop set to 6 posts per page.

    Everything works splendidly, I used the $do_not_duplicate trick I read from here, and set one of the arguments in the main loop to
    'post__not_in' => array($todays_post)

    I also us if(! is_paged()) to run and show the todays post latest post only on the first page.

    This works as well. All except for one nagging thing. When you go to page 2, the 6th (last post on page one) repeats as the first post on page 2. If I change the post__not_in to exclude the whole todays post category, the problem disappears. Everything is great.

    Also, if I remove the conditional on the top featured loop and allow it to appear on all pages, the problem disappears.

    Hopefully this is enough info, and any help is greatly appreciated.

    edit: here's the template in pastebin http://pastebin.com/kiT63zDA

    Also should add, I did use WP Query for the second loop originally, but tried query_posts to see if it made a difference, the results are exactly the same.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    You still need to query on your paginated pages for the 'post__not_in' id(s). This is an example to get the 'post__not_in' id(s) without showing the first loop on paginated pages:

    <?php
    $post__not_in = array();
    // first query
    $args =  array(
    	// your todays post query args
    );
    // query for todays post
    $todays_query = new WP_Query( $args );
    // check if there are posts
    if ( $todays_query->have_posts() ) : ?>
    <?php
    	// get posts ids for $post__not_in array
    	$post__not_in = wp_list_pluck( $todays_query->posts, 'ID' );
    // show post(s) on first page
    if ( !is_paged() ) : ?>
    		<?php while ( $todays_query->have_posts() ) : $todays_query->the_post(); ?>
    		<!-- do stuff inside the loop -->
    		<?php endwhile; ?>
    	<?php endif; ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
    <!-- second query -->

    For the second query you can now use the $post__not_in array like this:

    'post__not_in' => $post__not_in, // $post__not_in is an array

    [edit] I didn't see you provided a pastebin

  3. TimTee
    Member
    Posted 1 year ago #

    thx.

    I see in your code, the difference is, you run the todays post query, set the post not in id, -before- I have my conditional, effectively running the query and setting that post not in on each subsequent page.

    I have the whole query inside the conditional (so none of it runs on subsequent pages)

    Is that what I'm doing wrong?

    thanks!

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Is that what I'm doing wrong?

    Yes the second query needs the '$todays_post' id to exclude it (on the first page and paginated pages):
    try it with this [untested]: http://pastebin.com/heNe0YsY

  5. TimTee
    Member
    Posted 1 year ago #

    Ahhh! I just finished editing my template to what you suggested.

    Brilliant. Absolutely brilliant. Thank you very much for this!

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. Im glad you've got it resolved :-)

  7. TimTee
    Member
    Posted 1 year ago #

    what you suggested makes total sense. How would the subsequent pages know what the excluded post was?

    I spent hours googling and hunting this. It probably was staring me right in the face in those 'do_not_duplicate' code example too!

    thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic