如何在WordPress管理面板中制作可排序的栏目,用于统计页面印象数的自定义栏目?

时间:2015-03-20 作者:llgruff

早上好
我尝试为WordPress创建显示后计数器。

Output count of views

function getPostViews($postID){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, \'0\');
    return "0";    }
return $count.\'\';}

register view

function setPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, \'0\');
}else{
    $count++;
    update_post_meta($postID, $count_key, $count);    }}

output count of views in the admin panel

add_filter(\'manage_pages_columns\', \'posts_column_views\');
add_action(\'manage_pages_custom_column\', \'posts_custom_column_views\',10,2);
add_filter(\'manage_posts_columns\', \'posts_column_views\');
add_action(\'manage_posts_custom_column\', \'posts_custom_column_views\',10,2);
function posts_column_views($defaults){
    $defaults[\'post_views\'] = __(\'Views\');
    return $defaults;}
function posts_custom_column_views($column_name, $id){
    if($column_name === \'post_views\'){
        echo getPostViews(get_the_ID());    }}
一切正常

Adds the ability to sort column

add_filter(\'manage_edit-post_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-page_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-slides_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-hp_highlights_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-portfolio_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-staff_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-services_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-testimonials_sortable_columns\', \'add_views_sortable_column\');
add_filter(\'manage_edit-faqs_sortable_columns\', \'add_views_sortable_column\');
function add_views_sortable_column($sortable_columns){
  $sortable_columns[\'post_views\'] = \'views_views\';
  return $sortable_columns;}
可以按列排序“Views“,按字符串的形式为:/wp admin/edit.php?orderby=views\\u views&;order=desc,但实际上排序不在字段上”Views“,它在现场”Date

Change the query for sorting columns show WordPress, that need sorting by random fields whose values are numbers
(方法1)

add_filter(\'pre_get_posts\', \'add_column_views_request\');
function add_column_views_request( $object ){
  if( $object->get(\'orderby\') != \'views_views\' )
    return;
  $object->set(\'meta_key\', \'post_views\');
  $object->set(\'orderby\', \'meta_value_num\');}
(方法2)

add_action( \'pre_get_posts\', \'add_column_views_request\', 1 );
function add_column_views_request( $query ) {
  if ( $query->is_main_query() && ( $orderby = $query->get( \'orderby\' ) ) ) {
    switch( $orderby ) {
        case \'views_views\':
            $query->set( \'meta_key\', \'post_views\' );
            $query->set( \'orderby\', \'meta_value\' );
        break;}}}
(方法3)

add_filter(\'request\', \'add_column_views_request\');
function add_column_views_request( $vars ) {
    if ( \'views_views\' == $vars[\'orderby\'] ){
    $vars[\'meta_key\'] = \'post_views\';
    $vars[\'orderby\'] = \'meta_value_num\';}
    return $vars;}
排序仅返回无结果:“No entries found.“
请告诉我如何修复代码。

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

我只是想Method 1

 $object->set(\'meta_key\', \'post_views_count\');

Method 2

 $query->set( \'meta_key\', \'post_views_count\' );

Method 3

 $vars[\'meta_key\'] = \'post_views_count\';

结束

相关推荐

通过函数.php将样式嵌入到标题中

我正在尝试将以下挂钩添加到我的函数中。php文件。CSS样式在我查看源代码时显示出来,但它也显示随机背景图像生成器的php代码。我如何让这一行中的php在钩子中工作?谢谢add_action(\'wp_head\',\'hook_css\'); function hook_css() { $output=\" <style> body { background-rep