Including Future (Scheduled) Posts

This is a custom modification for the Count Posts in a Category plugin to allow the inclusion of future (scheduled) posts (or any post types other than “published”).

WordPress user TXALLDAY asks if this plugin (Count Posts in a Category) could be modified to include future (scheduled) posts. Yes! This is how:

Digression

(I decided against adding post_status as a shortcode attribute since few people would probably use it, and a much larger number would be confused by it. Plus, because this plugin uses get_term_by() to return an element with a built in (published) post count, it would make the code marginally efficient to have to run every request though get_posts() every time, or else I’d have to add separate functions, and it’s already pretty inefficient. But if a few people request this feature, I will find a way to add it… But I digress.)

Introduction

The easiest way to do this is just to add a new shortcode called [cat_count_future]. For simplicity’s sake, it only accepts a category ID with the id="" attribute. I’m not sure if TXALLDAY wanted just scheduled posts, or scheduled posts and published posts, so I’ve also included the attribute future_only="no" so it can do it either way. By default (if omitted) it will count both, unless you set future_only="yes", then it will count scheduled only.

Code

Open up the count_posts_in_cat.php file and add the following at the end (just before the closing ?>):

// category future posts count function
function cpina_future_count( $atts ) {
    // extract passed shortcode atts
    extract( shortcode_atts( array( 
        'id' => NULL,
        'future_only' => 'no' ), $atts ) ); 

    // count future + published or just future?
    $post_status = ( $future_only=='yes' ? 'future' : array( 'future', 'publish' ) );

    // build get_posts args
    $args = array(
        'numberposts' => 0, // unlimited # of results
        'category' => $id,
        'post_status' => $post_status );

    $posts = get_posts( $args ); // get matching posts
    return count( $posts ); // count matching posts
}
// register the shortcode 
add_shortcode( 'cat_count_future', 'cpina_future_count' );

Shortcode Use

Then use the shortcode [cat_count_future id="44" future_only="yes"] to count only scheduled posts in the category with the ID number 44. Or just [cat_count_future id="44"] to count published and scheduled posts in that category.

Note

It will not count posts which are “pending review”, but if you want it to, just add the pending post status type to the array. In fact, you can mix and match post_status types to make your own function. The valid possible post_statuses are:

  • publish – a published post
  • pending – post pending review
  • draft – a saved draft
  • auto-draft – an auto-saved (blank) draft
  • future – a scheduled post
  • private – a private post
  • inherit – a revision (incremental save)
  • trash – a trashed post

If enough people want this functionality, I will include it in the next version of the default plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>