0

I have declared a selection of custom post types as an array, allowing post_tags but not categories:

// Add Multiple Custom Post Types

add_action('init', 'all_custom_post_types');

function all_custom_post_types() {
  $types = array(
    array('the_type' => 'mama',
      'single' => 'Mama',
      'plural' => 'Mamas'),
    array('the_type' => 'webinar',
      'single' => 'Webinar',
      'plural' => 'Webinars'),
    array('the_type' => 'video',
      'single' => 'Video',
      'plural' => 'Videos'),
    array('the_type' => 'discount',
      'single' => 'Discount',
      'plural' => 'Discounts'),
    array('the_type' => 'faq',
      'single' => 'FAQ',
      'plural' => 'FAQs'),
  );

foreach ($types as $type) {
  $the_type = $type['the_type'];
    $single = $type['single'];
    $plural = $type['plural'];
    $labels = array(
      'name' => _x($plural, 'post type general name'),
      'singular_name' => _x($single, 'post type singular name'),
      'add_new' => _x('Add New', $single),
      'add_new_item' => __('Add New '. $single),
      'edit_item' => __('Edit '.$single),
      'new_item' => __('New '.$single),
      'view_item' => __('View '.$single),
      'search_items' => __('Search '.$plural),
      'not_found' =>  __('No '.$plural.' found'),
      'not_found_in_trash' => __('No '.$plural.' found in Trash'),
      'parent_item_colon' => ''
    );
    $args = array(
      'labels' => $labels,
      'public' => true,
      'publicly_queryable' => true,
      'show_ui' => true,
      'query_var' => true,
      'rewrite' => true,
      'capability_type' => 'post',
      'hierarchical' => false,
      'has_archive' => true,
      'menu_position' => 5,
      'supports' => array('title','editor','thumbnail','author','comments','excerpt'),
      'show_in_menu' => true,
      'show_in_nav_menus' => true,
      'exclude_from_search' => true,
      'taxonomies' => array( 'post_tag' ),
    );
    register_post_type($the_type, $args);
  }
}

As can be seen, the arguments allow for public => true; and has_archive => true;.

I've also set up a custom taxonomy to work as Categories for CPTs only:

function topics() {
    $labels = array(
        'name'              => _x( 'Topics', 'taxonomy general name' ),
        'singular_name'     => _x( 'Topic', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Topics' ),
        'all_items'         => __( 'All Topics' ),
        'parent_item'       => __( 'Parent Topic' ),
        'parent_item_colon' => __( 'Parent Topic:' ),
        'edit_item'         => __( 'Edit Topic' ), 
        'update_item'       => __( 'Update Topic' ),
        'add_new_item'      => __( 'Add New Topic' ),
        'new_item_name'     => __( 'New Topic' ),
        'menu_name'         => __( 'Topics' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'has_archive' => true,
    );
    register_taxonomy( 'topics', array( 'discount', 'video', 'webinar' ), $args );
}
add_action( 'init', 'topics', 0 );

My problem is that when calling the archive pages for these The CPTs are simply not being found. Tags shared by CPTs and standard posts return only the latter, and topics archives or tag archives with only CPTs assigned return empty.

I can see from my markup that the correct templates are being called (tag.php, taxonomy.php) but all are empty.

Why would this be happening?

1 Answer 1

1

Tags shared by CPTs and standard posts return only the latter, and topics archives or tag archives with only CPTs assigned return empty.

As for the tag archives, that's a default behavior in WordPress core, i.e. tag (post_tag taxonomy) and category (category taxonomy) archives will only query posts where the type is post.

However, the pre_get_posts hook can be used to include custom post types in the main posts query on tag and category archives, like so for tag archives and your CPTs:

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_tag() ) {
        $query->set( 'post_type', array( 'post', 'mama', 'webinar', 'video',
                                         'discount', 'faq' ) );
    }
} );

As for the topics archives, they're empty because you set exclude_from_search to true, which not only excludes the post type from search results, but also taxonomy/term archives.

Excerpt from the documentation: (formatting by me)

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_domain/?taxonomy_slug=term_slug or site_domain/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…

So to fix the topics archives, either set the exclude_from_search to false, or you could use the pre_get_posts hook like the example for tag archives:

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_tax( 'topics' ) ) {
        $query->set( 'post_type', array( 'discount', 'video', 'webinar' ) );
    }
} );
1
  • 1
    fantastic answer sally – thank you so much. it not only works, but i learned something, not much more to ask for! m
    – mtm
    May 6, 2022 at 8:52

Your Answer

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

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