Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For some reason the wp_counts_posts function returns an empty object if I use my custom post type's name in the function's parameter. The var_dump is:

object(stdClass)#93 (0) { }

The name of my cpt is CommunityUpload and I use the count function as follows:

function community_gallery_post_count(){
$posts = wp_count_posts( 'communityupload' );
var_dump( $posts );
return $posts->publish;

The function returns as expected if counting standard posts.

I define my cpt as follows:

[...]
var $single = "Community Upload";   // this represents the singular name of the post type
var $plural = "Community Uploads";  // this represents the plural name of the post type
var $type   = "CommunityUpload";    // this is the actual type
function add_post_type(){
$labels = array(
  'name' => _x($this->plural, 'post type general name'),
  'singular_name' => _x($this->single, 'post type singular name'),
  'add_new' => _x('Add ' . $this->single, $this->single),
  'add_new_item' => __('Add New ' . $this->single),
  'edit_item' => __('Edit ' . $this->single),
  'new_item' => __('New ' . $this->single),
  'view_item' => __('View ' . $this->single),
  'search_items' => __('Search ' . $this->plural),
  'not_found' =>  __('No ' . $this->plural . ' Found'),
  'not_found_in_trash' => __('No ' . $this->plural . ' found in Trash'),
  'parent_item_colon' => ''
);
$options = array(
  'labels' => $labels,
  'public' => true,
  'publicly_queryable' => true,
  'show_ui' => true,
  'query_var' => true,
  'rewrite' => array('slug' => $this->slug),
  'capability_type' => 'post',
  'hierarchical' => false,
  'has_archive' => true,
  'menu_position' => null,
  'supports' => array(
    'title',
    'editor',
    'author',
    'thumbnail',
    'excerpt',
    'comments',
    'custom-fields'
  ),
);
register_post_type($this->type, $options);
}
[...]

Any help is appreciated, thank you.

share|improve this question
    
Why are you using var and are you using this code inside a class? –  Pieter Goosen Jun 26 at 12:21
    
When is the function community_gallery_post_count called? –  engelen Jun 26 at 12:23
    
@PieterGoosen no real reason for var, just a way of storing a variable, why? Yes, it is inside a class that gets instantiated in child theme's functions.php –  Sergey Jun 26 at 12:28
    
@engelen the function is called inside bp_core_new_nav_item function, that I call to create custom BuddyPress menu item with 'name' => __( sprintf('My Gallery(%d)', community_gallery_post_count()), 'buddypress' ) –  Sergey Jun 26 at 12:32
    
And on what action do you call bp_core_new_nav_item? –  engelen Jun 26 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.