My plugin adds a row in the wp_postmeta table whenever a post is liked:
meta_id | post_id | meta_key | meta_value
27526 | 179 | liker | 177
27527 | 182 | liker | 343
27528 | 182 | liker | 360
...
(the meta_value stores the ID of the user which liked this post).
How do I use WP_Query to query all posts, ordered by number of likes?
EDIT: This is how to do it in SQL, but I need the WP_Query equivalent.
SELECT post_id, COUNT(*) AS likes
FROM wp_postmeta
WHERE meta_key='liker'
GROUP BY post_id
ORDER BY likes DESC
Check Wordpress Documentation
You could use the parameter orderby: with the string meta_key
I think you may use the SQL...
Only IDs:
And then query like this:
I've have 3 pages...
And then...
If I run...
The output is:
If I run...
The output is: