Adding Custom Post Type Counts To Your Dashboard

More than likely when you login into your WordPress Admin the first thing that you look at is the amount of posts, pages, categories, and tags being displayed in the “Right Now” meta box. Well, lets be honest you might look and you might not. I guess it would depend on how honest you are with yourself on your lack of publishing.

Nevertheless, now that we have added custom post types to WordPress, It would be nice to have a number display with your custom post type stats in the right now box in your dashboard. So again you can monitor how much you aren’t publishing these either. To do this we are going to open back up our custom post type class and add a new function. If you haven’t read that tutorial don’t feel bad, just feel out of the loop as you read this tutorial or get up to speed: custom post type class.

Step One: Copy WordPress.org

Well, there is no sense why we should to try to code our own function because WordPress.org already has it done: Right Now Content Table There you will find the code we are going to take and paste into our class. After, coping the code, I am going to open our class and paste this at the end.

Step Two: Replace Bits and Pieces With Your Class Varaibles

All we have to do is then find that parts that we can potentially replace with class variables. Here is the code redone for our class:

function add_count() {

  if (!post_type_exists($this->type)) {
     return;
  }

  $num_posts = wp_count_posts( $this->type );
  $num = number_format_i18n( $num_posts->publish );
  $text = _n( $this->single, $this->plural, intval($num_posts->publish) );

  if ( current_user_can( 'edit_posts' ) ) {
    $num = "<a href='edit.php?post_type=" . $this->type . "'>$num</a>";
    $text = "<a href='edit.php?post_type=" . $this->type . "'>$text</a>";
  }

  echo '<td class="first b ' . $this->plural . '">' . $num . '</td>';
  echo '<td class="t ' . $this->plural . '">' . $text . '</td>';
  echo '</tr>';

  if ($num_posts->pending > 0) {
    $num = number_format_i18n( $num_posts->pending );
    $text = _n( $this->single . ' Pending',  $this->plural . ' Pending', intval($num_posts->pending) );
    if ( current_user_can( 'edit_posts' ) ) {
      $num = "<a href='edit.php?post_status=pending&post_type=" . $this->type . "'>$num</a>";
      $text = "<a href='edit.php?post_status=pending&post_type=" . $this->type . "'>$text</a>";
    }

    echo '<td class="first b ' . $this->plural . '">' . $num . '</td>';
    echo '<td class="t ' . $this->plural . '">' . $text . '</td>';
    echo '</tr>';
  }
}

Also, I renamed the function ‘add_count’.

Step Three: Use add_action to call our funciton

The last thing we need to do is to take the code where created the custom post type and add one last hook like this:

add_action('right_now_content_table_end', array($event_post_type, 'add_count'));

If you are not sure where this is coming from you did need to read the previous post. But now if we got everything else right you will see this:

As you see on the bottom in the right now box from my dashboard, the row for “events” was added. Looks like I have 6 events, that is because I am popular.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
This entry was posted in Tricks. Bookmark the permalink.

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>