Skip to content

Instantly share code, notes, and snippets.

@renshuki
Last active November 1, 2021 18:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renshuki/41d0f67edf4dc36a69b8e3186138fdad to your computer and use it in GitHub Desktop.
Save renshuki/41d0f67edf4dc36a69b8e3186138fdad to your computer and use it in GitHub Desktop.
Wordpress SQL query to find posts having both tags

Please replace TAG ID 1 and TAG ID 2 by the tags you are looking for.

SELECT p.id
FROM wp_posts AS p
JOIN wp_term_relationships AS tr ON p.id = tr.object_id
JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms AS t ON tt.term_id = t.term_id
WHERE t.term_id=_TAG ID 1_ OR t.term_id=_TAG ID 2_ AND p.post_type LIKE 'post'
GROUP BY p.id
HAVING COUNT( * ) >1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment