0

I have a code which sorts the content by most liked, but I would like it to show the most liked in between previous week monday and next week monday. And the posts must be published in that time frame. Tho I have no idea how to sort by published date and most liked in that time frame. Grateful for help if anyone can. Using Ulike plugin to pull likes.

$loop = new WP_Query(
    array(
        'posts_per_page' => 6,
        'post_status' => 'publish',
        'post_type' => 'post',
        'orderby' => 'meta_value_num',
        'meta_key' => '_liked',
            'date_query' => array(
            array(
                'after' => strtotime( 'monday previous week' ),
                'before' => strtotime( 'monday next week' )
            ),
        ),
        'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
    )
);
while ($loop -> have_posts()) : $loop -> the_post();

1 Answer 1

1

WordPress allows multiple orders with the orderby parameter. https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

'meta_key' => '_liked', // You do it well
'orderby'  => array('meta_value_num' => 'DESC', 'date' => 'DESC'),

Here you tell WordPress to sort by "bigger" likes and last publish date.

2
  • Semi-works, now it's still showing the posts before previous week monday unfortunately. I think my date query is wrong somehow? Commented Dec 14, 2017 at 20:52
  • Thanks I just finished it. My date query was wrong. 'orderby' => array('meta_value_num' => 'DESC', 'date' => 'DESC'), 'date_query' => array( array( 'after' => 'monday previous week', 'before' => 'monday next week' ), ), Commented Dec 14, 2017 at 21:03

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.