• Resolved pk-71

    (@pauljohnknight)


    Hi,

    I have a custom post type and in the CPT’s cpt-single.php file I would like to pull in two posts instead of one.

    The two posts will be the post the user clicked in the relevant archive, and the next post in date order (i.e. the default post sorting method of WordPress). The reason for this is the posts are essentially small, useful pieces of information and having two posts pulled in will create a better SEO and user experience.

    Normally when I want to pull in a set number of posts on an archive page I would use WP_Query() and set 'posts_per_page' => 2 but out of the box this won’t work on a cpt-single.php file because such code pulls in posts that are the most recent, not the post that was clicked on the archive page (and then the next most recent).

    What I’m looking for is something that works with the WP loop so each post looks the same, but pulls in two posts (the selected one from the archive and then the next one in date order).

    <?php 
    $newsArticles = new WP_Query(array(
        'posts_per_page' => 2,
        'post_type'=> 'news'
    ));
    while(  $newsArticles->have_posts()){
            $newsArticles->the_post(); ?>
    
    // HTML content goes here
    
    <?php } ?>
    <?php wp_reset_postdata(); ?>

    Any help would be amazing.

    Emily

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t think it’s possible to get a named post and its following post through a single WP_Query instance. You first need to get the named post so that the date is available for finding its following post. Maybe it’s possible with a single custom compound query, but not with WP_Query.

    Thus, I recommend you leave the main, single query alone and add a new secondary query (and loop) to get only the following post. Use the “date_query” query var to get one post whose date is before the date of the main query’s post. Because date order is descending, you’ll get the post with the closest date.

    Thread Starter pk-71

    (@pauljohnknight)

    Hi @bcworkz would you be able to give some pointers / code to look at for me to get started on? The WP_Query code is a snippet I’ve always used, but I’m still only really at basic to intermediate level so am at a bit of a loss where to start with the above advice. Thanks for taking the time to answer.

    give this a try:

    <?php
    	$current_id = get_the_ID();
    	$next_post = get_next_post();
    	$next_id = $next_post->ID;
    	$cpt = get_post_type();
    	$cpt_array = array($current_id, $next_id);
    	$args = array(
    	   'post_type' => $cpt,
    	   'post__in' => $cpt_array,
    	   'order_by' => 'post_date',
    	   'order' => 'ASC',
    	);
    	$the_query = new WP_Query($args);
    	if($the_query->have_posts()):
    		while($the_query->have_posts() ): $the_query->the_post();
    		   echo '<h2>'.the_title().'</h2>';
    		endwhile;
    	endif;
    	wp_reset_postdata();
    ?>

    I didn’t test it a lot but locally, it indeed gets the current posts ID, gets the next posts ID then queries it and orders the two by date.

    Thread Starter pk-71

    (@pauljohnknight)

    Hi @tugbucket Thanks for this answer it worked !

    I realised when I used your answer I actually wanted the selected post as the first of the two and then the previous post so I changed the next posts etc to get_previous_post(); and did the order as 'DESC'

    Are you on StackOverflow because my other half also posted this question on there and she put a 100 point bounty on it so if you are you can just cut and paste your answer in and we’ll give you the 100 bounty points.

    Thanks again for taking the time to help !

    @pauljohnknight cool. glad it helped. I cross posted on SE as well. Man that’s been a while ??

    Thread Starter pk-71

    (@pauljohnknight)

    @tugbucket thanks. You should’ve got the bounty too.

    One other thing, do you know how to solve the issue in the link below? It’s a side effect of having two posts on the single-cpt.php page.

    https://www.ads-software.com/support/topic/make-previous_post_link-function-show-the-post-after-next-i-e-jump-a-post/

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Pull In Two Posts To Custom Post Type `cpt-single.php` File With WP_Query()’ is closed to new replies.