在管理界面中更新帖子数(已发布,草稿,未附加)


8

我正在使用多用户WordPress安装程序,因此已经使它成为一种特定类型的用户,只能看到他们编写的帖子,图像,页面等并与之交互。实现此目的的代码如下所示:

add_filter('pre_get_posts', 'current_author_posts');

function current_author_posts($query) {

    if ($query->is_admin && current_user_can('artist')) {
        global $user_ID;
        $query->set('author', $user_ID);
    }

    return $query;
}

这在管理界面中效果很好,但所有帖子计数显示不正确:

该图片显示了WordPress中不正确的帖子数

您是否知道任何过滤器或挂钩可以操纵这些数字,并且在各个帖子,页面,媒体和自定义帖子类型中都是正确的?

非常感谢。


wp_count_posts 过滤器
Mort 1305

Answers:


8

我几乎可以正常工作了,但是需要进行改进以适应问题的具体情况,并以不同方式处理附件和帖子类型(请参见代码中的注释)...


首先,我认为应该注意如何找到过滤器:
apply_filters( 'views_' . $screen->id, $views )

  • 检查元素
    子子子

  • 做一个全局搜索中/wp-admin,并/wp-includessubsubsub(有趣的类名,BTW)...

  • 这是: /wp-admin/includes/class-wp-list-table.php

foreach( array( 'edit-post', 'edit-page', 'edit-movie', 'upload' ) as $hook )
    add_filter( "views_$hook" , 'wpse_30331_custom_view_count', 10, 1);

function wpse_30331_custom_view_count( $views ) 
{
    global $current_screen;
    switch( $current_screen->id ) 
    {
        case 'edit-post':
            $views = wpse_30331_manipulate_views( 'post', $views );
            break;
        case 'edit-page':
            $views = wpse_30331_manipulate_views( 'page', $views );
            break;
        case 'edit-movie':
            $views = wpse_30331_manipulate_views( 'movie', $views );
            break;
        case 'upload':
            $views = wpse_30331_manipulate_views( 'attachment', $views );
            break;
    }
    return $views;
}

function wpse_30331_manipulate_views( $what, $views )
{
    global $user_ID, $wpdb;

    /*
     * This is not working for me, 'artist' and 'administrator' are passing this condition (?)
     */
    if ( !current_user_can('artist') ) 
        return $views;

    /*
     * This needs refining, and maybe a better method
     * e.g. Attachments have completely different counts 
     */
    $total = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending') AND (post_author = '$user_ID'  AND post_type = '$what' ) ");
    $publish = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = '$user_ID' AND post_type = '$what' ");
    $draft = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = '$user_ID' AND post_type = '$what' ");
    $pending = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'pending' AND post_author = '$user_ID' AND post_type = '$what' ");

    /*
     * Only tested with Posts/Pages
     * - there are moments where Draft and Pending shouldn't return any value
     */
    $views['all'] = preg_replace( '/\(.+\)/U', '('.$total.')', $views['all'] ); 
    $views['publish'] = preg_replace( '/\(.+\)/U', '('.$publish.')', $views['publish'] ); 
    $views['draft'] = preg_replace( '/\(.+\)/U', '('.$draft.')', $views['draft'] ); 
    $views['pending'] = preg_replace( '/\(.+\)/U', '('.$pending.')', $views['pending'] ); 

    // Debug info
    //echo 'Default counts: <pre>'.print_r($views,true).'</pre>';
    //echo '<hr><hr>';
    //echo 'Query for this screen of this post_type: <b>'.$what.'</b><pre>'.print_r($wp_query,true).'</pre>';

    return $views;
}

1
非常感谢这个。救了我的屁股。我有一个问题:是否可以完全删除.subsubsub中包含的菜单?
Maciej Gurban

4

在撰写本文时,此功能现在位于类“ WP_List_Table”中的“ views()”方法中。

过滤器现在看起来像这样:

$views = apply_filters( "views_{$this->screen->id}", $views );

$ views将包含每个列表元素的数组:

[19-Feb-2016 11:43:44 UTC] Array
(
  [all] => <a href="link_to_view" class="current">Alle <span class="count">(1)</span></a>
  [trash] => <a href="link_to_view">Trash <span class="count">(94)</span></a>
  [confirmed] => <a href="link_to_view">Confirmed <span class="count">(1)</span></a>
)

您可以将其挂载到current_screen挂钩,优先级> 10:

add_action( 'current_screen', function ( $current_screen ) {
        if ($current_screen->id === 'edit-my_page')
            add_filter( "views_{$current_screen->id}", 'list_table_views_filter' );
    }, 20);

function list_table_views_filter( array $view ) {
    error_log(print_r($view, true));
    return $view;
}

然后,您可以在列表中的元素上添加/更改/删除。


0

我知道这是一篇过时的文章,但是我已经多次浏览它以寻找更好的解决方案,因此我认为我会在其中添加一些内容。直到今天,我一直在从中筛选出的字符串中解析出每个数字view_{$screen->id}。但是,我刚刚了解到这些数字已在class-wp-posts-list-table.php的248行中解析。该view_{$screen->id}过滤器应该用于添加删除视图,而不是更改数字(愚蠢的我)。

用于执行视图字符串括号中所有数字计算的函数为wp_count_posts()。而且,它得到了相同名称的过滤器,因为这一直是各地的3.7.0版(发布于2013年10月24日,两年后,这一问题被问)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.