早上好
我尝试为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.“
请告诉我如何修复代码。