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

时间:2011-10-05 作者:Scott

我正在开发一个多用户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;
}
这在管理界面中运行良好,但会导致所有帖子计数显示不正确:

image that shows incorrect post counts within WordPress

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

非常感谢。

4 个回复
最合适的回答,由SO网友:brasofilo 整理而成

我几乎做到了这一点,但需要改进以适应问题的具体情况,并以不同的方式处理附件和帖子类型(请参见代码中的注释)。。。

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

检查元件subsubsub

在中进行全局搜索/wp-admin/wp-includes 对于子subsubsub(有趣的类名,顺便说一下)。。。

这里是:/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;
}

SO网友:DarkNeuron

在撰写本文时,此功能现在驻留在类WP\\u List\\u Table的方法“views()”中。

过滤器现在如下所示:

$views = apply_filters( "views_{$this->screen->id}", $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>
)
您可以将其挂接在当前的\\u屏幕挂钩上,优先级>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;
}
然后可以添加/更改/删除列表中的元素。

SO网友:Mort 1305

我知道这是一篇老帖子,但我已经多次遇到它,寻找更好的解决方案,所以我想我应该在其中添加一些东西。直到今天,我一直在从过滤的字符串中解析出每个单独的数字view_{$screen->id}. 但是,我刚刚知道这些数字是在248 of class-wp-posts-list-table.php. 这view_{$screen->id} 过滤器应该用于添加删除视图,而不是更改数字(愚蠢的我)。

对视图字符串括号中的数字进行所有计算的函数是wp_count_posts(). 而且,它有a filter with the same name 从那以后就一直存在version 3.7.0 (2013年10月24日发布,该问题提出两年后发布)。

SO网友:RafaSashi

有2个选项。假设您的自定义帖子类型键为;工艺美术运动“;

OPTION 1

使用挂钩views_{$this->screen->id} 此处:

https://developer.wordpress.org/reference/hooks/views_this-screen-id/

你应该这样钩住:

add_filter( \'views_edit-arts-and-crafts-movement\', \'filter_list_table_views\', 10 );
然后你应该在某处添加一个名为filter_list_table_views 要过滤视图,请执行以下操作:

function filter_list_table_views( $views ){

   // your counting logic here

   return $views;
}

OPTION 2

使用挂钩wp_count_posts 此处:

https://developer.wordpress.org/reference/functions/wp_count_posts/

add_action( \'current_screen\', function ( $screen ) {
            
    if( $screen->id == \'edit-arts-and-crafts-movement\' ){
            
        add_filter( \'wp_count_posts\', \'filter_list_table_counts\' );

    }
});

function filter_list_table_counts( $counts ){

   // your counting logic here

   return $counts;
}

结束

相关推荐

阻止admin部分(但仍使用admin-ajax.php)

我正在mo建立一个基于社区的WP站点,并阻止任何禁止管理员使用该站点上的管理部分的人:add_action( \'init\', \'sw_block_users\' ); function sw_block_users() { if ( is_admin() && ! current_user_can( \'administrator\' ) ) { wp_redirect( home_url() ); e