Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active December 20, 2015 11:19
Show Gist options
  • Save tatat/6122906 to your computer and use it in GitHub Desktop.
Save tatat/6122906 to your computer and use it in GitHub Desktop.
WordPressの投稿につけられてる全部のタグを投稿数付きで持ってくるにはこれでいいんですかね..
<?php
$query = "
SELECT t.*, (
SELECT COUNT(p.ID) FROM {$wpdb->term_relationships} r
INNER JOIN {$wpdb->posts} p
ON r.object_id = p.ID
WHERE
r.term_taxonomy_id = t.term_id AND
p.post_type = 'post' AND
p.post_status = 'publish'
) AS `count`
FROM {$wpdb->terms} t
WHERE
t.term_id IN (
SELECT tt.term_id FROM {$wpdb->term_taxonomy} tt
WHERE
tt.taxonomy = 'post_tag'
GROUP BY tt.term_id
)
ORDER BY t.name ASC
";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment