WordPress.org

Ready to get started?Download WordPress

Forums

Posts Per Category
[resolved] Posts Per Category by date range (5 posts)

  1. ConstantSorrow
    Member
    Posted 1 year ago #

    Great plugin.
    I'm wondering if it would be possible to also limit the number of posts per page by say, posts created within the past 7 days?

    I like being able to limit the posts per category separately from other the default number of posts shown, but I would also like to limit the number shown to the most recent content of the past week.

    Doable?

    Thanks for your plugin.

    http://wordpress.org/extend/plugins/posts-per-category/

  2. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    This is possible, but there are a number of ways that the list of posts can be modified, as well as various ways that a feature like this could be implemented, so I'm not sure it's a good candidate for integration into this particular plugin at the moment.

    That being said, if you just want all categories to only show posts from the last 7 days and you're comfortable dropping a small snippet of code into your functions.php file, we can probably come up with something.

  3. ConstantSorrow
    Member
    Posted 1 year ago #

    Thanks for your response.
    I would like to try the code snippet if you could come up with it :)
    Again thanks for your work.

  4. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    It ended up being a little more code than I thought (mostly comments to explain how it works), but let me know if this does what you want.

    - Brady

    /**
     * Attach a hook during main category queries.
     *
     * Determine if the current query is the main query and if it's for a
     * category archive. If so, select all posts and attach a hook to filter
     * the SQL WHERE clause. All posts are retrieved in case there are more
     * posts in the past week than the number shown by default.
     *
     * @param object $query WP_Query object passed by reference.
     */
    function constantsorrow_pre_get_posts( $query ) {
    	if ( ! is_admin() && is_category() && $query->is_main_query() ) {
    		// Show all posts.
    		$query->set( 'posts_per_page', -1 );
    
    		// Attach hook to filter WHERE clause.
    		add_filter( 'posts_where', 'constantsorrow_limit_to_past_week' );
    
    		// Remove the filter after it is executed.
    		add_action( 'posts_selection', 'constantsorrow_remove_limit_to_past_week_filter' );
    	}
    }
    add_action( 'pre_get_posts', 'constantsorrow_pre_get_posts' );
    
    /**
     * Filter the SQL WHERE clause to limit the query to grabbing posts from the
     * past week.
     *
     * @param string $where SQL WHERE clause passed from the filter.
     * @return string Modified WHERE clause.
     */
    function constantsorrow_limit_to_past_week( $where ) {
    	// The date 7 days ago.
    	// Use current_time() to calculate based on WordPress time instead of server time.
    	$date = date( 'Y-m-d', ( current_time( 'timestamp' ) - 60 * 60 * 24 * 7 ) );
    
    	// Append fragment to WHERE clause to select posts newer than the past week.
    	$where .= " AND post_date>='" . $date . "'";
    
    	return $where;
    }
    
    /**
     * Remove the filter limiting posts to the past week.
     *
     * Remove the filter after it runs so that it doesn't affect any other
     * queries that might be performed on the same page (eg. Recent Posts
     * widget).
     */
    function constantsorrow_remove_limit_to_past_week_filter() {
    	remove_filter( 'posts_where', 'constantsorrow_limit_to_past_week' );
    }
  5. ConstantSorrow
    Member
    Posted 1 year ago #

    Great. I will try it this weekend and let you know.
    Can't thank you enough!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic