Skip to content

Instantly share code, notes, and snippets.

@Clorith
Created December 5, 2014 07:26
Show Gist options
  • Save Clorith/2843d156df053e584393 to your computer and use it in GitHub Desktop.
Save Clorith/2843d156df053e584393 to your computer and use it in GitHub Desktop.
Get any WordPress post without any tags
<?php
global $wpdb;
$entries = $wpdb->get_results( $wpdb->prepare( "
SELECT
p.ID,
t.term_id
FROM
" . $wpdb->posts . " p
LEFT JOIN
(
SELECT
term_rel.object_id,
term_rel.term_taxonomy_id,
t.taxonomy,
t.term_id
FROM
" . $wpdb->term_relationships . " term_rel
JOIN
" . $wpdb->term_taxonomy . " t
ON ( t.term_taxonomy_id = term_rel.term_taxonomy_id )
WHERE
t.taxonomy = %s
GROUP BY
term_rel.object_id
) t
ON ( t.object_id = p.ID )
WHERE
t.term_id IS NULL
AND
p.post_type = %s
", 'post_tag', 'post' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment