Taxonomy Does Not Query Post Type
-
I’ve run into this situation multiple times, it seems to happen sporadically, and I’m not sure why it happens.
# The Situation
I register my taxonomy before my post type. I log in, assign a term to my custom post, view the term archive page and the SQL generated by WP_Query includes all other post types except the one it’s actually assigned to. The custom taxonomy/post type is registered within the theme oninit
.# Debugging
I’ve put the code into a MU plugin, enabled Twenty Nineteen, disabled all plugins, same situation. The generated SQL does not include the assigned post type. If I usepre_get_posts
and manually assign the post type the query functions as expected.# Example Code
As a MU Plugin:/** * Create Taxonomies * * @return void */ function prefix_tax_init() { // Makes Categories register_taxonomy( 'tax_makes', array( 'cpt_cars' ), array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_tagcloud' => false, 'rewrite' => array( 'slug' => 'cars/make', 'hierarchical' => true, 'with_front' => false ), ) ); } add_action( 'init', 'prefix_tax_init' ); /** * Create Post Type * * @return void */ function prefix_cpt_init() { // Cars Custom Post Type register_post_type( 'cpt_cars', array( 'labels' => $labels, 'taxonomies' => array( 'tax_makes' ), 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' =>true, 'show_ui' => true, 'query_var' => true, 'show_in_nav_menus' => false, 'capability_type' => 'page', 'hierarchical' => false, 'has_archive' => 'cars', 'menu_position' => 4, 'rewrite' => array( 'slug' => 'cars/car', 'with_front' => false ), 'supports' => array( 'title', 'editor' ) ) ); } add_action( 'init', 'prefix_cpt_init' );
# Example Query
This is what happens if I spit out $wp_query at the top of the template. It has the correct term, correct taxonomy, correct taxonomy ID but it just does not include the post type:SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (13) ) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
– – – – – – – – –
Am I missing something on the registration? Is
init
not the best hook for registering new types? I’m open to suggestions on what may cause this.
- The topic ‘Taxonomy Does Not Query Post Type’ is closed to new replies.