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
1 Answer
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/