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');   
});
1
  • What's the algorithm? It's impossible to say how to do it without knowing what you actually need to do. Aug 17, 2020 at 14:34

1 Answer 1

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.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.