最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to create algorithm for ordering posts in Wordpress?

programmeradmin2浏览0评论

I have a news feed in my website where all the blog posts are displayed. Wordpress default is to dislay these in order "latest first". Using Elementor and Likebtn I have learned how to order the posts based on likes by creating a query ID "sort_by_likes" in Elementor Post Widget and inputting the code below in my functions.php file. But I want to use a more advanced algorithm for displaying my posts which weights in number of views, likes, date of publication and some randomization variable. How can I solve that?

add_action('elementor/query/sort_by_likes', function($query){
    
    $query->set('orderby', 'meta_value');
    $query->set('meta_key', 'likes');   
});

I have a news feed in my website where all the blog posts are displayed. Wordpress default is to dislay these in order "latest first". Using Elementor and Likebtn I have learned how to order the posts based on likes by creating a query ID "sort_by_likes" in Elementor Post Widget and inputting the code below in my functions.php file. But I want to use a more advanced algorithm for displaying my posts which weights in number of views, likes, date of publication and some randomization variable. How can I solve that?

add_action('elementor/query/sort_by_likes', function($query){
    
    $query->set('orderby', 'meta_value');
    $query->set('meta_key', 'likes');   
});
Share Improve this question edited Aug 17, 2020 at 14:33 Jacob Peattie 44.1k10 gold badges50 silver badges64 bronze badges asked Aug 16, 2020 at 10:21 raenraen 1 1
  • What's the algorithm? It's impossible to say how to do it without knowing what you actually need to do. – Jacob Peattie Commented Aug 17, 2020 at 14:34
Add a comment  | 

1 Answer 1

Reset to default 0
$orderby = array( 
               'likes'      => 'DESC', 
               'views' => 'DESC',
               'date' => 'DESC',
); 


$query->set('orderby', $orderby);

Note: Make sure meta_key match with your meta keys.

Here is the reference: https://make.wordpress/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

发布评论

评论列表(0)

  1. 暂无评论