Custom Post Types not showing on Custom Taxonomy archive page

I’ve been working with custom post types for a little while now, but yesterday I took my first stab at a custom taxonomy to pair with them.

Here is my ‘company’ taxonomy:

function create_company_taxonomy() {
    $labels = array(
        'name'              => _x( 'Companies', 'taxonomy general name' ),
        'singular_name'     => _x( 'Company', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Companies' ),
        'all_items'         => __( 'All Companies' ),
        'parent_item'       => __( 'Parent Company' ),
        'parent_item_colon' => __( 'Parent Company:' ),
        'edit_item'         => __( 'Edit Companies' ),
        'update_item'       => __( 'Update Companies' ),
        'add_new_item'      => __( 'Add New Company' ),
        'new_item_name'     => __( 'New Company Name' ),
        'menu_name'         => __( 'Company' ),
    );

    $args = array(
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'company' ),
        'public' => true,
        'hierarchical' => true,
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'query_var' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true
    );

    register_taxonomy( 'company', array( 'brochures','business-cards','post','websites' ), $args );
}
add_action( 'init', 'create_company_taxonomy', 0 );

As you can see I am trying to use it with ‘posts’ as well as three custom post types–‘brochures’,’business-cards’,’websites’

When I create a taxonomy-company.php template (based off archive.php) and give each of these post_types a term from the taxonomy, only posts display on the page. I cannot get the custom post types to display at all.

Do I need a custom query? Any insight would be appreciated.

EDIT: Here is one of my register_post_types as well

add_action( 'init', 'create_custom_post_types' );
function create_custom_post_types() {

   $labels = array(
    'name' => __( 'Websites' ),
    'singular_name' => __( 'Website' )
    );

    $args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'websites'),
    'taxonomies' => array( 'category','company','post_tag' ),
    'supports'  => array( 'title', 'editor', 'thumbnail' , 'custom-fields', 'excerpt' ),
    'exclude_from_search' => true
    );

  register_post_type( 'websites', $args);
}

1
1

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