Find Untagged Posts in WordPress

It’s common for people to categorize or tag their posts when using WordPress. This is true for the average blogger and for those who are using WordPress for its content management features.

For example, let’s say that you’re responsible for working on a site that has an RSVP custom post type and the post types can be tagged for certain types of events.

Tags may include:

  • Formal
  • Informal
  • Wedding
  • Birthday
  • Family
  • Friends
  • …and so on

Overtime, the database is going to increase in size and if RSVPs aren’t manually entered (that is, they are imported or maybe the database is even inherited and mismanaged), there’s a chance that some of the posts will not be tagged.

If you’re a developer, the odds are strong that you’re going to need to at least locate the untagged posts (and perhaps update them, as well).

Programmatically Find Untagged Posts in WordPress

As with anything that’s related to programming, there are a number of ways to do this, but if you’re working with WordPress then you’re best bet is to use WP_Query.

If you’re working with the standard post types (like, you know, all published posts), then you won’t need to be as explicit in your code as I am below, but if you’re working with taxonomies – custom or not – as well as custom post types, then here’s how you can setup an instance of WP_Query to find all of the posts that  aren’t tagged with one of more IDs.

In short, the code retrieves all public posts that do not have a tag with the ID of 10, 15, 20, 25, or 30. Straightforward enough, right?

Note that there’s a big assumption made in the code above: It’s that you know the IDs of the taxonomies for which you want to query. Sometimes, it may just be a subset of the taxonomies that exist on the site; other times, it may be all of the IDs.

In the code, the IDs of the tags are defined in the array that’s being passed as the terms argument. You can read more about that in the Codex.

Whatever the case, there are ways to retrieve that information programmatically, but it’s best left for another post.

2 Replies to “Find Untagged Posts in WordPress”

  1. Tom, you elected not to use:

    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘mercury’, ‘venus’, ‘mars’ ),

    I always like to use the slug of an entity since slugs are easier to remember. Just a personal preference?

    1. I always like to use the slug of an entity since slugs are easier to remember. Just a personal preference?

      Yep – no other reason. Sometimes I use slugs but if I say, have run a query that’s returned an array of IDs, then I’d just use that.

      In the case of what inspired this post, that’s exactly what I had done so that’s what I opted to go with. Using slugs can be fine, too.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.