1

I'm trying to sort posts based on one post meta value and date. I'm using this plugin ( https://github.com/JonMasterson/WordPress-Post-Like-System ) to add like system to my posts.

The code that I've is as follows.

$args = array(
    'numberposts' => 15,
    'offset' => 0,
    'category' => 0,
    'meta_key' => '_post_like_count',
    'orderby' => 'meta_value_num post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'suppress_filters' => true 
);

$recent_posts = wp_get_recent_posts( $args );

The plugin adds a meta key _post_like_count to the table if at least one person like the post. The above code just returns posts which have at least one like count ( say 3 out of 15 ).

How do I get the remaining posts which don't have the meta key and sort them based on the date?

1

If you want both posts that have and do not have a meta key, you need an OR relation meta query to select both posts with key and those where it does not exist.

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => '_post_like_count',
            'compare' => 'EXISTS'
        ),
        array(
            'key' => '_post_like_count',
            'compare' => 'NOT EXISTS'
        )
    ),
    'orderby' => 'meta_value_num post_date',
    'order' => 'DESC'
);

$recent = new WP_Query( $args );
  • This seems to be working partially. Now I'm getting all the posts, but ordering is a little messed up. The posts with 2 likes on Dec 5 comes first and then few posts published between Nov 7 and Dec 5 which have no likes at all and then posts before Nov 7 which have one like each. Basically I want to list posts in order of max number of likes first(a) and then based on date if the posts don't have any likes after (a) – Arjun S Kumar Jan 10 '16 at 12:01
  • @ArjunSKumar I can't reproduce that issue, ordering works correctly when I test locally. var_dump( $recent ) after the query is run and look at $recent->request to inspect the SQL query. Note that you have to be using WP_Query to do this, not wp_get_recent_posts. – Milo Jan 10 '16 at 16:43
  • I think the issue is when there are equal number of likes for a few posts. They are coming in the end and sorted based on the dates, and yes, I'm using WP_Query itself – Arjun S Kumar Jan 11 '16 at 8:09
  • @ArjunSKumar again, I can't reproduce that issue. Have you inspected the request and query vars and made sure no other code is altering it? – Milo Jan 11 '16 at 18:17
  • Here's the request query : SELECT wp_posts.* FROM wp_posts LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key = '_post_like_count' ) WHERE 1=1 AND ( wp_postmeta.meta_key = '_post_like_count' OR mt1.post_id IS NULL ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.post_date DESC " – Arjun S Kumar Jan 12 '16 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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