我搜索了网站,但没找到我要找的东西。我有一个正常运行的代码,只有输出错误,这就是为什么我认为它不起作用。
到目前为止,这是我的代码:
这是函数:
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 View';
}
return $count.' Views';
}
// function to count views.
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);
}
}
// Add it to a column in WP-Admin - (Optional)
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,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());
}
}
$args = array( 'numberposts' => 4, /* get 4 posts, or set -1 for all */
'orderby' => 'meta_value_num', /* this will look at the meta_key you set below */
'meta_key' => 'post_views_count',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$myposts = get_posts( $args );
foreach( $myposts as $mypost ) {
$id = $mypost->ID;
$post_views = intval($post->views);
$post_title = get_the_title($post);
$post_title = $post->post_title;
}
提前非常感谢你。
答案 0 :(得分:0)
尝试将'orderby' => 'meta_value_num'
更改为'orderby' => 'meta_value meta_value_num'
或'orderby' => 'meta_value_num meta_value'
。我看到这个解决方案适用于某些人。
我实际上有一个非常类似的问题。尝试转储SQL(这里的教程:http://ben.lobaugh.net/blog/45390/wordpress-how-to-view-the-sql-query-generated-by-wp_query)并查看它的外观。我遇到这个问题时发现SQL错了。