我有以下代码,目前显示post\\u类型的所有帖子product
在wp admin的用户部分的Art专栏中。(略微修改自https://pippinsplugins.com/add-user-id-column-to-the-wordpress-users-table/)
add_filter(\'manage_users_columns\', \'pippin_add_user_id_column\');
function pippin_add_user_id_column($columns) {
$columns[\'art\'] = \'Art\';
return $columns;
}
add_action(\'manage_users_custom_column\', \'pippin_show_user_id_column_content\', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
query_posts(array(
\'post_type\' => \'product\',
\'author\' => $user_id,
) );
$count = 0;
while (have_posts()) : the_post();
$count++;
endwhile;
// echo $count;
$art = $count;
wp_reset_query();
if ( \'art\' == $column_name ) {
$pwc_link = admin_url() . "edit.php?post_type=product&author=".$user_id;
return \'<a href="\'.$pwc_link.\'" target="_blank">\'.$art.\'</a>\';
}
return $value;
}
我找到了这个教程,它解释了如何对管理列进行排序
http://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable--wp-25095 之后我添加了以下代码:
add_filter( \'manage_users_sortable_columns\', \'my_sortable_cake_column\' );
function my_sortable_cake_column( $columns ) {
$columns[\'art\'] = \'art\';
//To make a column \'un-sortable\' remove it from the array
//unset($columns[\'date\']);
return $columns;
}
但我现在一直坚持的部分是告诉WordPress如何对数据进行排序。他们使用的示例是:
add_action( \'pre_get_posts\', \'my_slice_orderby\' );
function my_slice_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( \'orderby\');
if( \'slice\' == $orderby ) {
$query->set(\'meta_key\',\'slices\');
$query->set(\'orderby\',\'meta_value_num\');
}
}
但是,我不知道在哪里可以按自定义帖子类型计数排序,也没有meta\\u键或查询。。。所以我有点困了。我确信这是一个使用post count进行另一个query\\u posts的情况,但我如何根据它对结果排序?
任何帮助都将是巨大的,我所要做的就是在您单击“艺术”列时按艺术(自定义贴子类型产品)计数订购用户。
最合适的回答,由SO网友:Rachel Carden 整理而成
用户的查询方式与帖子不同,因此您必须挂接“pre\\u user\\u query”操作以过滤用户查询。让我知道这是否有效:
add_action( \'pre_user_query\', \'my_pre_user_query\', 1 );
function my_pre_user_query( $query ) {
global $wpdb, $current_screen;
// Only filter in the admin
if ( ! is_admin() )
return;
// Only filter on the users screen
if ( ! ( isset( $current_screen ) && \'users\' == $current_screen->id ) )
return;
// Only filter if orderby is set to \'art\'
if ( isset( $query->query_vars ) && isset( $query->query_vars[ \'orderby\' ] )
&& ( \'art\' == $query->query_vars[ \'orderby\' ] ) ) {
// We need the order - default is ASC
$order = isset( $query->query_vars ) && isset( $query->query_vars[ \'order\' ] ) && strcasecmp( $query->query_vars[ \'order\' ], \'desc\' ) == 0 ? \'DESC\' : \'ASC\';
// Order the posts by product count
$query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} products WHERE products.post_type = \'product\' AND products.post_status = \'publish\' AND products.post_author = {$wpdb->users}.ID ) {$order}";
}
}