0

Created CPT = news

uses default categories (IT and recruitment) I am struggling to view an archive of the posts based on their category

mysite/news/ - shows all posts for that CPT whatever the category - good

mysite/news/recruitment - 404

mysite/category/recruitment - 404

mysite/news/category/recruitment - 404

(not sure which one of the last 3 should work)

Here is the code I am using - I imagine I'm missing the basics somewhere - thanks

add_action( 'init', 'cptui_register_my_cpts' );
   function cptui_register_my_cpts() {
   $labels = array(
     "name" => "News",
     "singular_name" => "News",
   );

$args = array(
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "show_ui" => true,
    "has_archive" => false,
    "show_in_menu" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => array( "slug" => "news", "with_front" => true ),
    "query_var" => true,

    "taxonomies" => array( "category" )
);
register_post_type( "news", $args );

// End of cptui_register_my_cpts()
}

1 Answer 1

0

Hope you did pretty url on your site already (Settings>Permalinks). If so, Let's guess your Category base is blank there.

You have to add Custom posts in category archive using a filter. To do so, add this to function.php:

function go_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'nav_menu_item', 'your-custom-post-type-here'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'go_add_custom_types' );

In default setup, WordPress Structure act like this (Based on observation on my blog)

For Default category,

site.com/category/catgory-name

or site.com/category-name

For custom taxonomy (by chance)

site.com/custom-taxonomy-slug/category-name

For single news (CPT)

site.com/news/post-name.

For single Default post:

site.com/<depends-on-permalink-structure>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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