Custom Post Types not showing on Custom Taxonomy archive page

By default, all public post types are included in the main main query on taxonomy pages. If you look at register_post_type()'spublic parameter when set to true

  • ‘true’

  • Implies exclude_from_search: false, publicly_queryable: true, show_in_nav_menus: true, and show_ui:true.

When you register your post type, you set exclude_from_search to true. This does not only remove the custom post type from search, but also from the main query on the taxonomy page. Again, from the codex for the exclude_from_search parameter

Note: If you want to show the posts’s list that are associated to taxonomy’s terms, you must set exclude_from_search to false (ie : for call site_domaine/?taxonomy_slug=term_slug or site_domaine/taxonomy_slug/term_slug). If you set to true, on the taxonomy page (ex: taxonomy.php) WordPress will not find your posts and/or pagination will make 404 error.

SOLUTION

You need to set exclude_from_search to false, reflush your permalinks and your good to go. If you need to exclude a post type from the search page, you can use pre_get_posts to remove the post type from the main query

EDIT

Just for interest sake, here is the piece of code in the WP_Query class (in wp-includes/query.php) which is responsible to include and exclude post types from the main query on taxonomy term archive pages

if ( $this->is_tax ) {
    if ( empty($post_type) ) {
        // Do a fully inclusive search for currently registered post types of queried taxonomies
        $post_type = array();
        $taxonomies = array_keys( $this->tax_query->queried_terms );
        foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
            $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
            if ( array_intersect( $taxonomies, $object_taxonomies ) )
                $post_type[] = $pt;
        }
        if ( ! $post_type )
            $post_type="any";
        elseif ( count( $post_type ) == 1 )
            $post_type = $post_type[0];

        $post_status_join = true;
    } elseif ( in_array('attachment', (array) $post_type) ) {
        $post_status_join = true;
    }
}

The important part here is: get_post_types( array( 'exclude_from_search' => false ). It is here where the query gets all public post types where exclude_from_search is set to false. All post types therefor with exclude_from_search set to true will be excluded from the query, and that is why your custom post types does not get included in the taxonomy term archive page

Leave a Comment