Basic CPT Question About Categories

The question:

Missing something basic here. Here’s my CPT code…

// Create a custom post type
//
add_action( 'init', 'custom_post_type_func' );
function custom_post_type_func() {
    //posttypename = PDSH Posts
    $labels = array (
    'name' => _x( 'PDSH Posts', 'pdsh_posts' ),
    'singular_name' => _x( 'PDSH Post', 'pdsh_posts' ),
    'add_new' => _x( 'Add New', 'pdsh_posts' ),
    'add_new_item' => _x( 'Add New PDSH Post', 'pdsh_posts' ),
    'edit_item' => _x( 'Edit PDSH Post', 'pdsh_posts' ),
    'new_item' => _x( 'New PDSH Post', 'pdsh_posts' ),
    'view_item' => _x( 'View PDSH Post', 'pdsh_posts' ),
    'search_items' => _x( 'Search PDSH Posts', 'pdsh_posts' ),
    'not_found' => _x( 'No PDSH Posts found', 'pdsh_posts' ),
    'not_found_in_trash' => _x( 'No PDSH Posts found in Trash', 'pdsh_posts' ),
    'parent_item_colon' => _x( 'Parent PDSH Post:', 'pdsh_posts' ),
    'menu_name' => _x( 'PDSH Posts', 'pdsh_posts' ),
    );
    $args = array (
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Hi, this is my custom post type.',
    'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
    'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
    );
    register_post_type( 'pdsh_posts', $args );
}

Works great!
I’m using the WP All Import plugin to create 3378 PDSH Posts with three parent categories: Alphabetical, Creators, Morality. They all look correctly populated here…
Basic CPT Question About Categories
But when I view one of those categories the archive page can’t see the posts, goes to content-none, throws up a Not Found.
Do I need to register all 106 categories somehow, or maybe just the three parent categories?

Updated:

Here’s my attempt to register “Alphabetical” and “A”…

//
// Hook into the init action and call create_book_taxonomies when it fires
// 
add_action( 'init', 'create_post_types_hierarchical_taxonomy', 0 );
// Create a custom taxonomy name it subjects for your posts
function create_post_types_hierarchical_taxonomy() {
    // Add new taxonomy, make it hierarchical like categories
    // //first do the translations part for GUI
  $labels = array(
    'name' => _x( 'Alphabetical', 'taxonomy general name' ),
    'singular_name' => _x( 'Alphabetical', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Alphabetical' ),
    'all_items' => __( 'All Alphabetical' ),
    'parent_item' => __( 'Parent Alphabetical' ),
    'parent_item_colon' => __( 'Parent Alphabetical:' ),
    'edit_item' => __( 'Edit Alphabetical' ), 
    'update_item' => __( 'Update Alphabetical' ),
    'add_new_item' => __( 'Add New Alphabetical' ),
    'new_item_name' => __( 'New Alphabetical Name' ),
    'menu_name' => __( 'Alphabetical' ),
  );
    // Now register the taxonomy
    register_taxonomy('Alphabetical',array('A'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'Alphabetical' ),
    ));
}

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It might just be the late hours, but the register_taxonomy call looks wrong. Shouldn’t the second parameter be the post type you’re registering the taxonomy for? I.e. array('pdsh_posts').

And yes, you need to register each of the three taxonomies separately for the post type.

See the codex example, https://developer.wordpress.org/reference/functions/register_taxonomy/#comment-397

Edit

Actually, its probably not what I wrote above. Registering the cpt with the default category taxonomy, if that is what you want to use, should only need the following parameter for the cpt

'taxonomies' => array( 'category' ),

The reason why you’re getting not found on the category view is that by default the Loop only looks for posts, not your cpts’. You’ll need to add the cpt to the query with pre_get_posts

Example here, https://wordpress.stackexchange.com/a/114853/144392


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment