Im working on a plugin. I placed like button under each post so users can like the post. When user click the button it updates ‘likes_count’ post_meta field which i created. Now here is my question. I created an admin page and i want to list all the tags order by ‘likes_count’ post_meta field. Is it possible? Thanks.
2 Answers
$querystr = "
SELECT DISTINCT
-- post_meta.meta_value,
key2.name as tag_name,key2.slug as tag_slug FROM $wpdb->posts key1
LEFT JOIN $wpdb->term_relationships ON (key1.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->postmeta post_meta ON (key1.ID = post_meta.post_id)
LEFT JOIN $wpdb->terms key2 ON ($wpdb->term_relationships.term_taxonomy_id = key2.term_id)
LEFT JOIN $wpdb->term_taxonomy key3 ON ($wpdb->term_relationships.term_taxonomy_id = key3.term_id)
WHERE 1=1
AND post_meta.meta_key = 'likes_count'
AND post_meta.meta_value > 0
AND (key1.post_status="publish")
AND key3.taxonomy = 'post_tag'
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
print_r($pageposts);
This is the exact query for tags. I tested this and worked fine.
in below given you sample output
Array
(
[0] => stdClass Object
(
[tag_name] => Tag1
[tag_slug] => tag1
)
[1] => stdClass Object
(
[tag_name] => Tag2
[tag_slug] => tag2
)
)