php 按热门页面视图排序帖子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 按热门页面视图排序帖子相关的知识,希望对你有一定的参考价值。

// Add page view to each post
function count_post_views() { //Store post view count in post_meta table under 'post_views_count' meta key
if(is_single() && 'post' == get_post_type()){ //Check if this is a single post. You can add validation here for post_type, etc., later 
$postID = get_the_id(); //Get the current post's ID
$count_key = 'post_views_count'; //Set the meta_key to 'post_views_count'
$count = (int)get_post_meta($postID, $count_key, true); //Grab the current meta_value for the 'post_views_count' meta_key
$count +=1; //Increase the count by 1
update_post_meta($postID, $count_key, $count); //Update the 'post_views_count' meta_value with the new count
}
}
add_action('wp_head','count_post_views');


// get query
function most_popular_posts_query($query){
$query->set('meta_key','post_views_count'); //Set the post query's meta_key to 'post_views_count'
$query->set('orderby', 'meta_value'); //Order posts by the 'post_views_count'
$query->set('order','DESC'); //Order by highest count to lowest
//return
return $query; //Pass the modified query back to Elementor
}
add_action('elementor/query/most_popular_posts' , 'most_popular_posts_query');

以上是关于php 按热门页面视图排序帖子的主要内容,如果未能解决你的问题,请参考以下文章

将来自两个不同 wordpress 的帖子合并到一个按日期排序的帖子页面

Wordpress 在自定义帖子类型存档页面上按日期排序

php 按标题,ASC排序自定义帖子类型

php 按对象属性对对象数组进行排序。示例:查询后对Wordpress帖子进行排序

Vue全家桶web端社区项目

PHP WordPress - 按名称获取帖子或页面ID