This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Limit posts by date

  • Creator
    Topic
  • #7263
    0rca
    Participant

    Hi all,

    is there a way how to limit the posts by date? We are trying to show only posts from the last year (i.e. 365 days) but can’t get it to work. Right now we are using the shortcode, but tried with all 3 post list methods..

    Cheers,

    Michael

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #7264
    Jason Bobich
    Keymaster

    Hello Michael,

    I am not aware of a way with the WordPress query system to pull posts of a certain date range. (Maybe I’m wrong?) —

    https://codex.wordpress.org/Class_Reference/WP_Query

    The only thing I’d be aware of is to query the posts by the specific calendar year. So, in the custom query option for any method of displaying a post list, you’d do:

    year=2013

    https://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

    I’m not sure how else you’d do a date range without actually digging into the code and doing your own custom MySQL query filter like it shows at the bottom of that section I’m linking to.

    #7615
    0rca
    Participant

    Actually I have tried something from that page:

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    	// posts in the last 365 days
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-365 days')) . "'";
    	return $where;
    }
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $query_string );
    remove_filter( 'posts_where', 'filter_where' );

    but I couldn’t get it to work, but I guess that’s mainly because I am a coding noob 🙂
    So I asked a friend, but he couldn’t do it either.. 🙁

    #7776
    0rca
    Participant

    Would the above query work with Jumpstart /Barely Corporate? If yes, I’ll look for a php-programmer to implement it for me.

    #7789
    Jason Bobich
    Keymaster

    I apologize, but this is such hard question to answer just in general. There are different potential ways I guess you could go about it, but it all depends on specifically how you’re displaying posts. Whatever method you’re using to display your posts, you need to look at the source code and find a place to hook somewhere before the query to apply the “posts_where ” filter, and then find somewhere to hook after where you can remove that filter.

    Jump Start and Barely Corporate use the same framework. Granted, if you look at the functions for displaying post lists and paginated post lists currently in Jump Start, you’ll see a few more hooks you could tap into, but this only because Jump Start has a more recent version of the framework. In the next big update to Barley Corporate, everything will match again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The forum ‘Barely Corporate Responsive WordPress Theme’ is closed to new topics and replies.