8

I've registered a custom post type "object" and registered the taxonomy "post_tag" so that these objects can be tagged.

register_taxonomy_for_object_type('post_tag', 'object');

When I visit domain.com/tag/{object-slug} it does not find any post, although there are.

Here is how my CPT was declared:

register_post_type( 'objet', 
        array('labels' => array(/* removed for clarity */),
            'description' => __( 'blabla' ), 
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 3, 
            'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', 
            'rewrite'   => array( 'slug' => 'stock', 'with_front' => false ), 
            'has_archive' => 'stock',
            'capability_type' => 'post',
            'taxonomies' => array('post_tag'),
            'hierarchical' => false,
            'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'sticky')
        ) 
    );
1
  • You seem to be missing some code to add_action to start the post type in the code above. Also would it be possible for you to post the taxonomy registration code and the updated version of the post type here together?
    – sulfureous
    Commented Jul 28, 2013 at 1:45

1 Answer 1

23

Tag and Category archive queries default to querying only the post post type, to add your custom post type to those queries, you can use the pre_get_posts action:

function wpa_cpt_tags( $query ) {
    if ( $query->is_tag() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'object' ) );
    }
}
add_action( 'pre_get_posts', 'wpa_cpt_tags' );
2
  • Thanks. And to include categories in the same function as I needed to, the condition should be if( ($query->is_tag() || $query->is_category()) && $query->is_main_query() ){}
    – Jonny Perl
    Commented Oct 6, 2016 at 16:44
  • For anyone viewing this, it looks like the slugs within the array in the code provided represents every post type that will be included in the archive. Thus, if you want to include your post type in the archive, you simply need to add the slug to the array.
    – Squagem
    Commented Apr 18, 2017 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.