Display all posts and custom post types with one category

I created a message type with CPT UI :

 add_action( 'init', 'cptui_register_my_cpts_matratze' ); function cptui_register_my_cpts_matratze() { $labels = array( "name" => __( 'Matratzen', '' ), "singular_name" => __( 'Matratze', '' ), ); $args = array( "label" => __( 'Matratzen', '' ), "labels" => $labels, "description" => "", "public" => true, "publicly_queryable" => true, "show_ui" => true, "show_in_rest" => false, "rest_base" => "", "has_archive" => true, "show_in_menu" => true, "exclude_from_search" => false, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => false, "rewrite" => array( "slug" => "matratze", "with_front" => true ), "query_var" => true, "supports" => array( "title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "author", "page-attributes", "post-formats" ), "taxonomies" => array( "category", "post_tag" ), ); register_post_type( "matratze", $args ); // End of cptui_register_my_cpts_matratze() } 

However, when I want to access categories by reference in my Frontend, I do not receive messages.

For example, when you click "I get nothing":

Category

The message is included and has a DaMi category:

Message

Is my CPT UI Post Type incorrectly configured? Any suggestions what am I doing wrong?

+6
source share
3 answers

view here

By default, category pages on your WordPress site will only display by default, so you need to add your CPT before Wordpress requests posts by adding the pre_get_posts filter.

code added here:

 add_filter('pre_get_posts', 'query_post_type'); function query_post_type($query) { if( is_category() ) { $post_type = get_query_var('post_type'); if($post_type) $post_type = $post_type; else $post_type = array('nav_menu_item', 'post', 'matratze'); // don't forget nav_menu_item to allow menus to work! $query->set('post_type',$post_type); return $query; } } 
+3
source

When you use the standard Wordpress category for your custom post type, by default Wordpress will not display your custom post type posts on the category archive page. Thus, you need to modify the page request in the categories archive to add your own message type.

Please specify the following code in the function.php file. Remember to change your own message type name in the following code.

 function add_custom_post_types_to_tax( $query ) { if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) { // Include your custom post type $post_types = array( 'post', 'your_custom_type' ); $query->set( 'post_type', $post_types ); return $query; } } add_filter( 'pre_get_posts', 'add_custom_post_types_to_tax' ); 

Hope this helps you.

+2
source

For this

 function query_post_type($query) { if( is_category() ) { $post_type = get_query_var('post_type'); // post type get here if($post_type){ // no more code here for by default } else{ $post_types = array( 'post', 'your_custom_type' ); // custom type } $query->set('post_type',$post_type); return $query; } } add_filter('pre_get_posts', 'query_post_type'); 

I use this function and it will work. Any other comment for him.

+1
source

All Articles