• Resolved ConstantSorrow

    (@constantsorrow)


    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/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Brady Vercher

    (@bradyvercher)

    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.

    Thread Starter ConstantSorrow

    (@constantsorrow)

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

    Plugin Author Brady Vercher

    (@bradyvercher)

    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' );
    }
    Thread Starter ConstantSorrow

    (@constantsorrow)

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Posts Per Category by date range’ is closed to new replies.