最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Wordpress post count not showing for custom post type author

programmeradmin7浏览0评论

I've created a few custom post types based on Dave Rupert's boilerplate and then migrated existing posts to these newly created CPTs via Post Type Switcher plugin. I did not make any changes to the boilerplate apart from changing CPT name and support* attributes.

'supports' => array(
   'title',
   'editor',
   'author',
   'thumbnail',
   'excerpt',
   'comments'
),

Migrated posts correctly display their authors and it is possible to change posts' author. However, when I view author list in Users section, the post counter for non-admin authors is zero (0). Temporarily changing author role to admin and back again does not help to show the correct count of posts for individual authors.

I checked the database records as well (pls check view from wp_posts table below). Authors IDs seem to be correctly assigned to individual posts. Still I think I broke the post-author relation somewhere in the database, but I can figure out where.

Does anyone know how to tackle this issue?

I've created a few custom post types based on Dave Rupert's boilerplate and then migrated existing posts to these newly created CPTs via Post Type Switcher plugin. I did not make any changes to the boilerplate apart from changing CPT name and support* attributes.

'supports' => array(
   'title',
   'editor',
   'author',
   'thumbnail',
   'excerpt',
   'comments'
),

Migrated posts correctly display their authors and it is possible to change posts' author. However, when I view author list in Users section, the post counter for non-admin authors is zero (0). Temporarily changing author role to admin and back again does not help to show the correct count of posts for individual authors.

I checked the database records as well (pls check view from wp_posts table below). Authors IDs seem to be correctly assigned to individual posts. Still I think I broke the post-author relation somewhere in the database, but I can figure out where.

Does anyone know how to tackle this issue?

Share Improve this question edited Jul 24, 2014 at 15:43 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Jul 24, 2014 at 15:17 MetinMetin 231 silver badge4 bronze badges 2
  • By default, custom post types are not counted – Pieter Goosen Commented Jul 24, 2014 at 15:34
  • Thank You! Found this answer in a Google Search and it worked perfectly for my needs. – jaypeg Commented Apr 17, 2019 at 23:52
Add a comment  | 

1 Answer 1

Reset to default 4

This is an answer I re-porpoised from a question that was abandoned. I thought this will help you solve this issue

By default, all custom post types are excluded from the main query, that is why you don't see any posts when going to the front end author page. To accomplish this, you will need to modify the main query to include these post type/s. For this purpose, you are going to use the pre_get_posts action hook. You will also use the conditional tag is_author() to check that you are on the author archive page. (I've tested this code using my custom post types event_type and cameras, so just change this accordingly)

function custom_post_author_archive($query) {
    if ($query->is_author() && $query->is_main_query() )
        $query->set( 'post_type', array('event_type', 'cameras', 'post', 'page') );
}
add_action('pre_get_posts', 'custom_post_author_archive');

To tackle the problem of post count in the back end, I think it will be a good idea to unset the posts column in the authors page, and then to replace it with a new column that will count all the post types, ie event_type, post and page. You can edit this accordingly

Here you will make use of manage_users_columns and manage_users_custom_column which is not very well documented.

function add_extra_user_column($columns) { //Add CPT Column for events and remove default posts column
    unset($columns['posts']);
    return array_merge( $columns, 
              array('foo' => __('Posts')) );
}
add_filter('manage_users_columns' , 'add_extra_user_column');

function add_post_type_column( $value, $column_name, $id ) { //Print event_type value
  if( $column_name == 'foo' ) {
    global $wpdb;
    $count = (int) $wpdb->get_var( $wpdb->prepare(
      "SELECT COUNT(ID) FROM $wpdb->posts WHERE 
       post_type IN ('event_type', 'cameras', 'post', 'page') AND post_status = 'publish' AND post_author = %d",
       $id
    ) );

    if ( $count > 0 ) {
        $r = "<a href='edit.php?author=$id'>";
        $r .= $count;
        $r .= '</a>';
    } else {
        $r = 0;
    }

    return $r;
  }
}

add_filter( 'manage_users_custom_column', 'add_post_type_column', 10, 3 );
发布评论

评论列表(0)

  1. 暂无评论