Support » Fixing WordPress » WordPress show next 3 x number adjacent custom posts from existing

  • Hi

    I’ve asked this question elsewhere with no joy. The idea I think is simple. I want to take the current post position and then show the next 3 posts in that custom post type in my sidebar. Right now I am pulling the current post and excluding that post fine, but it’s not really what I am looking for. Thi sis my current code:-

    		$currentID = get_the_ID();
            $query = new WP_Query(array('post_type' => get_post_type($post->ID), 'posts_per_page' => 5, 'order' => 'DESC', 'orderby' => 'date', 'offset' => $offset, 'page' => $page, 'post__not_in' => array($currentID) ));
    while ( $query->have_posts() ) : $query->the_post();
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>  
        <?php endwhile; wp_reset_postdata(); ?>

    The reason I need to do this is because the posts will be number 1,2, 3 etc so when you are on post 3 I want to show 4, 5, 6.


Viewing 12 replies - 1 through 12 (of 12 total)
  • I’d try either using (and probably filtering the SQL generated by) get_next_post() or specifying a date_query parameter to your WP_Query like:

    	$currentID = get_the_ID();
    	$query = new WP_Query( array(
    		'date_query' => array(
    				'before'    => $post->post_date,
    				'inclusive' => true,
    		'post_type'      => $post->post_type,
    		'posts_per_page' => 3,
    		'order'          => 'DESC',
    		'orderby'        => 'date',
    		'post__not_in'   => array( $currentID )
    	) );
    	if ( $query->have_posts() ) :
    	<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	<?php endwhile; wp_reset_postdata(); ?>
    <?php endif; ?>

    Thanks for this. Right now it seems to be querying ‘posts’ and not my current custom post type though?

    replaced line for post_type with:-


    Need to test to confirm, but I think that works!

    Ah, unfortunately that doesn’t work jkovis…

    I am ordering by ‘menu_order’ but even by ‘date it only works for the first 3/4 posts for some reason. If I click on post 3 it hides post 3 and shows 1, 2, 4, but should show 4, 5, 6. If I click on 4 or past 4 then it always shows 1, 2, 3.

    I should say that if I can get this to show the ‘next’ 3 posts in order then I don’t need to hide the current post by id as that wouldn’t show anyway, but of course that would be easy if we can get the above working correctly….

    What do you have $offset set to? Does changing it help?

    Hi Cindy

    I’m currently trying jkovis code above. There is no offset set in the Query.


    Can you post the SQL query (stored in $query->request) being generated?

    Also, what page template are you putting this code in?

    I believe if you’ll set 'offset' => 4 in the WP_Query with ‘4’ being the post position you would like to start with, it will work. If you want to show posts 4, 5 and 6 set the offset to 4.

    Cindy, Setting the offset as you suggest doesn’t work as that always offsets from the first post. i need the offset to be relative to the current post / the post that is open.

    jkovis the code is being pulled via a short code generator which is placing the php on a single post page (in the sidebar) for the custom post. How can I get that SQL Query for you?

    How can I get that SQL Query for you?

    If this is a live site then add this before the if ( $query->have_posts() ) : line:

    printf( '<!-- SQL: %s -->', print_r( $query->request ) );

    and then view the source code to find the query inside the html comment.

    If not, add this before the if ( $query->have_posts() ) : line and reload the page:

    print_r( $query->request );

    SELECT SQL_CALC_FOUND_ROWS gudsgwm_25_posts.ID FROM gudsgwm_25_posts WHERE 1=1 AND gudsgwm_25_posts.ID NOT IN (547) AND gudsgwm_25_posts.post_type = 'expert_guides' AND (gudsgwm_25_posts.post_status = 'publish') ORDER BY gudsgwm_25_posts.menu_order ASC LIMIT 0, 3 - See more at:

    Got this working from an alternate post here:-

    it’s not a WordPress Custom Query, but it works

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘WordPress show next 3 x number adjacent custom posts from existing’ is closed to new replies.