• 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 on init.

    # 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 use pre_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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m suspicious of 'query_var' => true, because the Code Reference says

    ‘query_var’
    (string|bool) Sets the query_var key for this post type. Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. If specified as a string, the query ?{query_var_string}={post_slug} will be valid.

    Thread Starter Howdy_McGee

    (@howdy_mcgee)

    Should be fine, the default for it in WP_Post_Type is true.

    I tried removing the query var param and just letting it default but it’s the same kind of issue.

    OK, maybe not.
    It seems like the standard loop does not add custom post types, so it seems to be normal. I always had to use code to show custom post types in the standard places (which are built for Posts). The search query shows all types. The query using the post type query var obviously gets that post type. Otherwise, I think you have to add it in where you want to see it.

    Thread Starter Howdy_McGee

    (@howdy_mcgee)

    With attached taxonomies it should be able to retrieve the attached post type and in 90% of cases I don’t need to do any additional work to make posts show up on a term page. This issue only crops up every once in awhile so I’m not sure if it’s a database issue, rewrite issue somewhere, or implementation issue.

    I’ve tried searching trac for known bugs but haven’t run across anything specific :/

    With your values for rewrite, how does the regex distinguish between the archive and the taxonomy?

    Thread Starter Howdy_McGee

    (@howdy_mcgee)

    The permalink structure would turn out to be:

    Archive: /cars/
    Term: cars/make/{$term}
    Single Post: cars/car/{$post_name}

    So the keywords in between the archive base slug, the term slug, and the single slug would/should distinguish it between one-another.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Taxonomy Does Not Query Post Type’ is closed to new replies.