When I query posts, the results are incorrect

The most important thing to understand about CPT-onomies is that they are not stored in the database in the same manner as taxonomies.

So if your queries are returning incorrect results, it’s most likely 1 of 2 scenarios:

I’m using wp_get_object_terms() to get CPT-onomy terms and taxonomy terms together and the results are incorrect

If you are using CPT-onomies and taxonomies and are trying to retrieve both CPT-onomy and taxonomy term information in the same request, i.e. in wp_get_object_terms(), there’s a small chance WordPress might get a little confused. The easiest solution? When you are using wp_get_object_terms() or something similar, request ‘all’ or ‘all_with_object_id’ fields. If WordPress has to retrieve all of the term information, it eliminates the chance that a CPT-onomy or taxonomy term will be overwritten and lost in the shuffle.

I’m using get_posts() or the WordPress loop and the results are incorrect

CPT-onomies bear the same name as their custom post type counterparts, e.g. if you have an “actors” custom post type, it’s CPT-onomy is also named “actors”. With that said, pre-CPT-onomies, you may have had a custom taxonomy named “actors” and, although that taxonomy is no longer registered, your old taxonomy’s term information may still exist in your database. This is where WordPress gets a little confused because CPT-onomy information is stored differently than regular taxonomies. To fix the problem, all you have to do is remove the conflicting taxonomy information (by following the steps below). If that doesn’t solve your problem, please let me know.

How To Remove Conflicting Taxonomy Terms

  • If you do not have access to your database or wish to only deal with the WP admin:
    1. Open your functions.php file and register your old taxonomy. It doesn’t matter which post type you attach it to, you just need access to the taxonomy’s “edit” page. For the sake of this tutorial, we’ll pretend you’ve attached it to “Posts”.
    2. Open the “Posts” submenu, and click your taxonomy. Select the checkbox at the top left of the terms table and do a “bulk action” to delete all of the terms.
    3. Once you’ve removed all of the terms, you can “unregister” your taxonomy by removing the register_taxonomy() code from your CPT-onomy file. This should clear up any WordPress confusion.
  • If you have access to your database:
    1. Find the “term_taxonomy” table and take note of the “term_taxonomy_id” and “term_id” of all of the rows with your taxonomy, then delete these rows.
    2. Find the “terms” table and delete any of the rows that contain one of your noted “term_id”s.
    3. Find the “term_relationships” table and delete any of the rows that contain one of your noted “term_taxonomy_id”s.