Site logo

Archived topic

CPT not displaying in category archive pages

4 replies · Started by Yulia on June 1, 2022

Viewing posts 1–5 of 5

Hi there,

I've hit a wall trying to figure this out. Any input is greatly appreciated.

I have two custom post types: free and wpt_test. wpt_test uses custom rewrite slug test and archive slug test.
Both CPT's support WP core taxonomies: categories and tags.

I'm trying to display list of custom posts on categories page for each CPT with Content Block element.
The Content Block element is set to be displayed on all archive pages.

I have followed documentation to create CPT, but still unable to display posts.

Below is CPT php code for reference. I've generated CPT's with CPT UI plugin in testing environment then copied this code to php snippets on main site.

function cptui_register_my_cpts() {

	/**
	 * Post Type: Бесплатники.
	 */

	$labels = [
		"name" => __( "Бесплатники", "generatepress" ),
		"singular_name" => __( "Бесплатник", "generatepress" ),
		"menu_name" => __( "Бесплатники", "generatepress" ),
		"all_items" => __( "All Бесплатники", "generatepress" ),
		"add_new" => __( "Add new", "generatepress" ),
		"add_new_item" => __( "Add new Бесплатник", "generatepress" ),
		"edit_item" => __( "Edit Бесплатник", "generatepress" ),
		"new_item" => __( "New Бесплатник", "generatepress" ),
		"view_item" => __( "View Бесплатник", "generatepress" ),
		"view_items" => __( "View Бесплатники", "generatepress" ),
		"search_items" => __( "Search Бесплатники", "generatepress" ),
		"not_found" => __( "No Бесплатники found", "generatepress" ),
		"not_found_in_trash" => __( "No Бесплатники found in trash", "generatepress" ),
		"parent" => __( "Parent Бесплатник:", "generatepress" ),
		"featured_image" => __( "Featured image for this Бесплатник", "generatepress" ),
		"set_featured_image" => __( "Set featured image for this Бесплатник", "generatepress" ),
		"remove_featured_image" => __( "Remove featured image for this Бесплатник", "generatepress" ),
		"use_featured_image" => __( "Use as featured image for this Бесплатник", "generatepress" ),
		"archives" => __( "Бесплатник archives", "generatepress" ),
		"insert_into_item" => __( "Insert into Бесплатник", "generatepress" ),
		"uploaded_to_this_item" => __( "Upload to this Бесплатник", "generatepress" ),
		"filter_items_list" => __( "Filter Бесплатники list", "generatepress" ),
		"items_list_navigation" => __( "Бесплатники list navigation", "generatepress" ),
		"items_list" => __( "Бесплатники list", "generatepress" ),
		"attributes" => __( "Бесплатники attributes", "generatepress" ),
		"name_admin_bar" => __( "Бесплатник", "generatepress" ),
		"item_published" => __( "Бесплатник published", "generatepress" ),
		"item_published_privately" => __( "Бесплатник published privately.", "generatepress" ),
		"item_reverted_to_draft" => __( "Бесплатник reverted to draft.", "generatepress" ),
		"item_scheduled" => __( "Бесплатник scheduled", "generatepress" ),
		"item_updated" => __( "Бесплатник updated.", "generatepress" ),
		"parent_item_colon" => __( "Parent Бесплатник:", "generatepress" ),
	];

	$args = [
		"label" => __( "Бесплатники", "generatepress" ),
		"labels" => $labels,
		"description" => "",
		"public" => true,
		"publicly_queryable" => true,
		"show_ui" => true,
		"show_in_rest" => true,
		"rest_base" => "",
		"rest_controller_class" => "WP_REST_Posts_Controller",
		"rest_namespace" => "wp/v2",
		"has_archive" => true,
		"show_in_menu" => true,
		"show_in_nav_menus" => true,
		"delete_with_user" => false,
		"exclude_from_search" => false,
		"capability_type" => "post",
		"map_meta_cap" => true,
		"hierarchical" => false,
		"can_export" => true,
		"rewrite" => [ "slug" => "free", "with_front" => true ],
		"query_var" => true,
		"menu_position" => 5,
		"menu_icon" => "dashicons-filter",
		"supports" => [ "title", "editor", "thumbnail", "custom-fields", "revisions", "author", "page-attributes", "post-formats" ],
		"taxonomies" => [ "category", "post_tag" ],
		"show_in_graphql" => false,
	];

	register_post_type( "free", $args );

	/**
	 * Post Type: Тесты.
	 */

	$labels = [
		"name" => __( "Тесты", "generatepress" ),
		"singular_name" => __( "Тест", "generatepress" ),
		"menu_name" => __( "Тесты", "generatepress" ),
		"all_items" => __( "Все Тесты", "generatepress" ),
		"add_new" => __( "Add new", "generatepress" ),
		"add_new_item" => __( "Add new Тест", "generatepress" ),
		"edit_item" => __( "Edit Тест", "generatepress" ),
		"new_item" => __( "New Тест", "generatepress" ),
		"view_item" => __( "View Тест", "generatepress" ),
		"view_items" => __( "View Тесты", "generatepress" ),
		"search_items" => __( "Search Тесты", "generatepress" ),
		"not_found" => __( "No Тесты found", "generatepress" ),
		"not_found_in_trash" => __( "No Тесты found in trash", "generatepress" ),
		"parent" => __( "Parent Тест:", "generatepress" ),
		"featured_image" => __( "Featured image for this Тест", "generatepress" ),
		"set_featured_image" => __( "Set featured image for this Тест", "generatepress" ),
		"remove_featured_image" => __( "Remove featured image for this Тест", "generatepress" ),
		"use_featured_image" => __( "Use as featured image for this Тест", "generatepress" ),
		"archives" => __( "Тест archives", "generatepress" ),
		"insert_into_item" => __( "Insert into Тест", "generatepress" ),
		"uploaded_to_this_item" => __( "Upload to this Тест", "generatepress" ),
		"filter_items_list" => __( "Filter Тесты list", "generatepress" ),
		"items_list_navigation" => __( "Тесты list navigation", "generatepress" ),
		"items_list" => __( "Тесты list", "generatepress" ),
		"attributes" => __( "Тесты attributes", "generatepress" ),
		"name_admin_bar" => __( "Тест", "generatepress" ),
		"item_published" => __( "Тест published", "generatepress" ),
		"item_published_privately" => __( "Тест published privately.", "generatepress" ),
		"item_reverted_to_draft" => __( "Тест reverted to draft.", "generatepress" ),
		"item_scheduled" => __( "Тест scheduled", "generatepress" ),
		"item_updated" => __( "Тест updated.", "generatepress" ),
		"parent_item_colon" => __( "Parent Тест:", "generatepress" ),
	];

	$args = [
		"label" => __( "Тесты", "generatepress" ),
		"labels" => $labels,
		"description" => "",
		"public" => true,
		"publicly_queryable" => true,
		"show_ui" => true,
		"show_in_rest" => true,
		"rest_base" => "",
		"rest_controller_class" => "WP_REST_Posts_Controller",
		"rest_namespace" => "wp/v2",
		"has_archive" => "test",
		"show_in_menu" => true,
		"show_in_nav_menus" => true,
		"delete_with_user" => false,
		"exclude_from_search" => false,
		"capability_type" => "post",
		"map_meta_cap" => true,
		"hierarchical" => false,
		"can_export" => true,
		"rewrite" => [ "slug" => "test", "with_front" => true ],
		"query_var" => true,
		"menu_position" => 6,
		"menu_icon" => "dashicons-editor-spellcheck",
		"supports" => [ "title", "editor", "thumbnail", "excerpt", "custom-fields", "revisions", "author", "page-attributes", "post-formats" ],
		"taxonomies" => [ "category", "post_tag" ],
		"show_in_graphql" => false,
	];

	register_post_type( "wpt_test", $args );
}

add_action( 'init', 'cptui_register_my_cpts' );

Hi Yulia,

To clarify, have you rebuilt your permalinks?

To do as such, go to Settings > Permalinks and simply click on Save.

Kindly let us know how it goes.

Figured it out. It turned out to be a WordPress issue. Out of the box, WordPress does not include custom post types in the archives for the default categories and tags archives. Because of this, you need to add the post types to the query yourself.

This tutorial from CPT UI plugin provides instructions.

Looks like CPT's work just fine without custom single-cpt.php content-cpt.php templates, as required in Generatepress documentation.

Did that as well, thanks.

You’re welcome Yulia! Glad you got it to work!

This archived topic is closed to new replies.