Support » Fixing WordPress » Related posts (older posts in chronological order)

  • Resolved Imenicaa

    (@imenicaa)


    Hello folks!

    Under each of my blog posts i have listed 3 “Related Posts”.Currently, the 3 newest posts are displayed in that section.
    I was wondering how can i sort them to show 3 posts older than the user is currently reading.
    So for example, if the user is reading posts from 1.1.2013, he should see 3 posts written before that posts.

    Here is the code that i have:

    `$args = array(
    ‘numberposts’ => 3,
    ‘orderby’ => ‘date’,
    ‘category’ => $wpex_related_category[0]->cat_ID,
    ‘exclude’ => get_the_ID(),
    ‘offset’ => null,
    ‘no_found_rows’ => true,
    ‘suppress_filters’ => false, // WPML support
    );

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    This will be easy in WP 3.7:
    http://www.viper007bond.com/2013/08/27/date-queries-in-wordpress-3-point-7/
    http://codex.wordpress.org/Function_Reference/WP_Query#Date_Parameters

    For now you can try it with this in your theme’s functions.php:

    function after_current_post( $where ) {
    	global $post;
    	if ( isset( $post->post_date ) && $post->post_date ) {
    		global $wpdb;
    		$where .= " AND $wpdb->posts.post_date < '$post->post_date'";
    	}
    	return $where;
    }

    And this to get the posts in your theme template file:

    <?php
    add_filter( 'posts_where', 'after_current_post' );
    
    $args = array(
    	'posts_per_page' => 3,
    	'orderby' => 'date',
    	'category' => $wpex_related_category[0]->cat_ID,
    	'post__not_in' => array( get_the_ID() ),
    	'no_found_rows' => true,
    	'suppress_filters' => false, // WPML support
    );
    
    $posts = get_posts( $args );
    remove_filter( 'posts_where', 'after_current_post' );
    ?>

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Hey Keesiemeijer!

    Thanks a bunch, i tried it and it works 🙂

    All the best

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome 🙂

    I know its not the place to ask this, but is there any chance that you know how to show smiley in post excerpt?
    It shows properly on single post page, but its missing from homepage list…
    I tried “Advanced Excerpt” plugin, but when i enable showing of <img> tags, it shows both smiley and post image, and that messes up the page…

    Sorry for asking here, if you ever come to Croatia, im buying you a beer lol

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Related posts (older posts in chronological order)’ is closed to new replies.