• Given I am on a particular post, C, how do I wuery to find posts B and A, given that the posts were added in date order, A, B then C.

    Another example:

    News post 1, 2, 3 and 4, posted in that order, one a day.

    On post 2, I should see a link to post 1.

    On 4, I should see links to 3 and 2.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Oh so you want to show all previous posts to the post you are on by published date.

    $args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'date_query' => array(
    		array(
    			'before' => array(
    				'year'  => 2013,
    				'month' => 2,
    				'day'   => 28,
    			),
    		),
    );
    
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
    	echo '<ul>';
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    		echo '<li>' . get_the_title() . '</li>';
    	}
    	echo '</ul>';
    }

    Something like that where you need to put the before and after dates in from the post you are on.

    This this will prob be:

    the_date('Y')
     the_date('m')
     the_date('d')

    https://codex.ww.wp.xz.cn/Class_Reference/WP_Query#Date_Parameters

    Thread Starter mr_pablo

    (@mr_pablo)

    Seems to work!

    Oddly, If i got to a post where there are no posts made before it, it still pulls in some posts…

    Well the code above will show all before it not just those directly before so in theory you’ll always have results with the above code unless you are on your very first post.

    Thread Starter mr_pablo

    (@mr_pablo)

    Actually, it’s not working. Checking the query, the date bening checking is coming through as 1970, meaning the date is wrong.

    I am using ‘before’ => get_the_time()

    Which is a correct way of using the date_query before option. But it doesn’t seem to work

    Thread Starter mr_pablo

    (@mr_pablo)

    NVM.

    “before” does not accept Unix timestamps.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Find previous 2 posts based on current post’ is closed to new replies.