Custom Taxonomy not fetching posts from Custom Post Type

With the following code in functions.php, a Custom Post Type is created along with a custom taxonomy. But the taxonomy cannot fetch any posts.

// Career
function cpt_career() {

$labels = array(
'name' => _x( 'Career', 'Post Type General Name', 'foxconndrc' ),
'singular_name' => _x( 'Career', 'Post Type Singular Name', 'foxconndrc' ),
'menu_name' => __( 'Career', 'foxconndrc' ),
'name_admin_bar' => __( 'Career', 'foxconndrc' ),
'archives' => __( 'Archive', 'foxconndrc' ),
'parent_item_colon' => __( 'Parent', 'foxconndrc' ),
'all_items' => __( 'All', 'foxconndrc' ),
'add_new_item' => __( 'New open position', 'foxconndrc' ),
'add_new' => __( 'Add New', 'foxconndrc' ),
'new_item' => __( 'New carreer item', 'foxconndrc' ),
'edit_item' => __( 'Edit career', 'foxconndrc' ),
'update_item' => __( 'Update', 'foxconndrc' ),
'view_item' => __( 'View', 'foxconndrc' ),
'search_items' => __( 'Search', 'foxconndrc' ),
'not_found' => __( 'Not found', 'foxconndrc' ),
'not_found_in_trash' => __( 'Not found in trash', 'foxconndrc' ),
'featured_image' => __( 'Featured image', 'foxconndrc' ),
'set_featured_image' => __( 'Set featured image', 'foxconndrc' ),
'remove_featured_image' => __( 'Remove featured image', 'foxconndrc' ),
'use_featured_image' => __( 'Use as featured image', 'foxconndrc' ),
'insert_into_item' => __( 'Insert', 'foxconndrc' ),
'uploaded_to_this_item' => __( 'Uploaded', 'foxconndrc' ),
'items_list' => __( 'Items list', 'foxconndrc' ),
'items_list_navigation' => __( 'List navigation', 'foxconndrc' ),
'filter_items_list' => __( 'Filter items', 'foxconndrc' ),
);
$args = array(
'label' => __( 'Career', 'foxconndrc' ),
'description' => __( 'Career', 'foxconndrc' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'revisions', 'thumbnail' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-groups',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'rewrite' => array (
'slug' => 'career',
'with_front' => FALSE
),
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'career', $args );

}
add_action( 'init', 'cpt_career', 0 );

if ( ! function_exists( 'custom_taxonomy_career' ) ) {

// Register Custom Taxonomy
function custom_taxonomy_career() {

$labels = array(
'name' => _x( 'Locations', 'Taxonomy General Name', 'foxconndrc' ),
'singular_name' => _x( 'Location', 'Taxonomy Singular Name', 'foxconndrc' ),
'menu_name' => __( 'Locations', 'foxconndrc' ),
'all_items' => __( 'All Items', 'foxconndrc' ),
'parent_item' => __( 'Parent Item', 'foxconndrc' ),
'parent_item_colon' => __( 'Parent Item:', 'foxconndrc' ),
'new_item_name' => __( 'New Item Name', 'foxconndrc' ),
'add_new_item' => __( 'Add New Item', 'foxconndrc' ),
'edit_item' => __( 'Edit Item', 'foxconndrc' ),
'update_item' => __( 'Update Item', 'foxconndrc' ),
'view_item' => __( 'View Item', 'foxconndrc' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'foxconndrc' ),
'add_or_remove_items' => __( 'Add or remove items', 'foxconndrc' ),
'choose_from_most_used' => __( 'Choose from the most used', 'foxconndrc' ),
'popular_items' => __( 'Popular Items', 'foxconndrc' ),
'search_items' => __( 'Search Items', 'foxconndrc' ),
'not_found' => __( 'Not Found', 'foxconndrc' ),
'no_terms' => __( 'No items', 'foxconndrc' ),
'items_list' => __( 'Items list', 'foxconndrc' ),
'items_list_navigation' => __( 'Items list navigation', 'foxconndrc' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'career_location' ),
);
register_taxonomy( 'career_location', array( 'career' ), $args );

}
add_action( 'init', 'custom_taxonomy_career', 0 );

}

Please advice. Thanks!

  • Dimitris

    Hey there Karolina,

    hope you're doing good today! :slight_smile:

    Could you please use the following snippet instead, re-save your permalinks and check again?
    I've tested that in a local installation with TwentySeventeen theme, using a MU plugin.

    // Register Custom Post Type
    function career() {
    
    	$labels = array(
    		'name'                  => _x( 'Careers', 'Post Type General Name', 'text_domain' ),
    		'singular_name'         => _x( 'Career', 'Post Type Singular Name', 'text_domain' ),
    		'menu_name'             => __( 'Careers', 'text_domain' ),
    		'name_admin_bar'        => __( 'Career', 'text_domain' ),
    		'archives'              => __( 'Career Archives', 'text_domain' ),
    		'attributes'            => __( 'Career Attributes', 'text_domain' ),
    		'parent_item_colon'     => __( 'Parent Career:', 'text_domain' ),
    		'all_items'             => __( 'All Careers', 'text_domain' ),
    		'add_new_item'          => __( 'Add New Career', 'text_domain' ),
    		'add_new'               => __( 'Add New', 'text_domain' ),
    		'new_item'              => __( 'New Career', 'text_domain' ),
    		'edit_item'             => __( 'Edit Career', 'text_domain' ),
    		'update_item'           => __( 'Update Career', 'text_domain' ),
    		'view_item'             => __( 'View Career', 'text_domain' ),
    		'view_items'            => __( 'View Careers', 'text_domain' ),
    		'search_items'          => __( 'Search Career', 'text_domain' ),
    		'not_found'             => __( 'Not found', 'text_domain' ),
    		'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
    		'featured_image'        => __( 'Featured Image', 'text_domain' ),
    		'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
    		'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
    		'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
    		'insert_into_item'      => __( 'Insert into Career', 'text_domain' ),
    		'uploaded_to_this_item' => __( 'Uploaded to this Career', 'text_domain' ),
    		'items_list'            => __( 'Careers list', 'text_domain' ),
    		'items_list_navigation' => __( 'Careers list navigation', 'text_domain' ),
    		'filter_items_list'     => __( 'Filter Careers list', 'text_domain' ),
    	);
    	$args = array(
    		'label'                 => __( 'Career', 'text_domain' ),
    		'description'           => __( 'Career Description here', 'text_domain' ),
    		'labels'                => $labels,
    		'supports'              => array( ),
    		'taxonomies'            => array( 'location' ),
    		'hierarchical'          => true,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 5,
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => 'careers',
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'capability_type'       => 'page',
    	);
    	register_post_type( 'career', $args );
    
    }
    add_action( 'init', 'career', 0 );
    
    // Register Custom Taxonomy
    function locations() {
    
    	$labels = array(
    		'name'                       => _x( 'Locations', 'Taxonomy General Name', 'text_domain' ),
    		'singular_name'              => _x( 'Location', 'Taxonomy Singular Name', 'text_domain' ),
    		'menu_name'                  => __( 'Location', 'text_domain' ),
    		'all_items'                  => __( 'All Locations', 'text_domain' ),
    		'parent_item'                => __( 'Parent Location', 'text_domain' ),
    		'parent_item_colon'          => __( 'Parent Location:', 'text_domain' ),
    		'new_item_name'              => __( 'New Location Name', 'text_domain' ),
    		'add_new_item'               => __( 'Add New Location', 'text_domain' ),
    		'edit_item'                  => __( 'Edit Location', 'text_domain' ),
    		'update_item'                => __( 'Update Location', 'text_domain' ),
    		'view_item'                  => __( 'View Location', 'text_domain' ),
    		'separate_items_with_commas' => __( 'Separate Locations with commas', 'text_domain' ),
    		'add_or_remove_items'        => __( 'Add or remove Locations', 'text_domain' ),
    		'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    		'popular_items'              => __( 'Popular Locations', 'text_domain' ),
    		'search_items'               => __( 'Search Location', 'text_domain' ),
    		'not_found'                  => __( 'Not Found', 'text_domain' ),
    		'no_terms'                   => __( 'No Locations', 'text_domain' ),
    		'items_list'                 => __( 'Locations list', 'text_domain' ),
    		'items_list_navigation'      => __( 'Locations list navigation', 'text_domain' ),
    	);
    	$rewrite = array(
    		'slug'                       => 'career_location',
    		'with_front'                 => false,
    		'hierarchical'               => true,
    	);
    	$args = array(
    		'labels'                     => $labels,
    		'hierarchical'               => true,
    		'public'                     => true,
    		'show_ui'                    => true,
    		'show_admin_column'          => true,
    		'show_in_nav_menus'          => true,
    		'show_tagcloud'              => true,
    		'rewrite'                    => $rewrite,
    	);
    	register_taxonomy( 'location', array( 'career' ), $args );
    
    }
    add_action( 'init', 'locations', 0 );

    References:
    https://generatewp.com/post-type/
    https://generatewp.com/taxonomy/

    Warm regards,
    Dimitris