• Resolved Kyrian

    (@oloccina)


    Hi,
    I have created a custom post type and connected it to the default WordPress categories

    Now I have default WordPress default posts and custom post types assigned to the same category, but when I visit the category page it only lists the WordPress default posts.

    Is this a normal behavior and I should always create a separate taxonomy/category for custom post type or maybe there is a issue here?

    Because I’d really like to use the same taxonomy for the 2 post types….

    This is an example of my category page: https://aikyam.net/c/contenuti/spirito/

    It should list contents from my “video” custom post type (https://aikyam.net/video/) and from my WordPress posts (https://aikyam.net/articoli/) but it only shows the latter

    Thank you

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yes, this is pretty normal with the default category/tags archives. We wrote up this quick tutorial on how to handle, if you’re willing to try out some code solutions

    https://docs.pluginize.com/article/17-post-types-in-category-tag-archives

    We also provided some UI for this for a much less code based solution in CPTUI-Extended, our premium addon, but not sure if you want to go that route. I’ll leave that one up to you.

    Thread Starter Kyrian

    (@oloccina)

    Thanks a lot Michael

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome. Give it a go and let me know if you experience any issues I can help with. I’ll leave the support thread open until we get confirmation of success.

    Thread Starter Kyrian

    (@oloccina)

    I have just tested the below code in my functions.php file

    but what happens is that now the category page lists ONLY the custom post type and not the WordPress default posts.

    see: https://aikyam.net/c/contenuti/spirito/

    Am i missing something here?

    Thanks

    // CPT UI - add custom post type to default wordpress taxonomies
    
    function my_cptui_add_post_types_to_archives( $query ) {
    	// We do not want unintended consequences.
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;    
    	}
    
    	if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'vid' );
    
    		$query->set(
    	  		'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not from what I can see, you copy/pasted over pretty accurately and updated the correct spots.

    Just in case, and it may feel a little odd, but do we for sure have some default posts that have the “spirito” category, as opposed to perhaps just a “contenuti” category?

    Thread Starter Kyrian

    (@oloccina)

    Yep

    default posts
    https://nimb.ws/1D8nCH

    custom type posts
    https://nimb.ws/5i0G8h

    but I just tested with twenty twenty theme and it works, so is my theme issue (I use Uncode theme), I’ll have to check with them…

    Thanks again!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Keep me updated, and hopefully they have some good leads on their end.

    Thread Starter Kyrian

    (@oloccina)

    Hi Michael,
    I just wanted to update you on this.
    Unfortunately Uncode Theme does not support the possibility to display more than 1 post type in the same taxonomy/category.
    Pity! But I guess I’ll have to go a different way.

    Thanks again for your support

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

    Thread Starter Kyrian

    (@oloccina)

    Hey Michael,
    sorry to bother you again but today I was testing the possibility to show in the author archive page a CPT UI custom post type along with regular wordpress posts.
    I thought it was not possible, since doing this in category pages with my Uncode theme seemed to not be possible.

    But after testing this piece of code

    
    // CPT UI - add custom post type to author archive page
    
        function post_types_author_archives($query) {
            if ($query->is_author)
                    $query->set( 'post_type', array('vid','post') );
            remove_action( 'pre_get_posts', 'custom_post_author_archive' );
        }
        add_action('pre_get_posts', 'post_types_author_archives');

    it just worked flawlessly (see https://aikyam.net/author/nican/)

    So I am thinking if the code I was using to do the same thing in category pages was maybe not right. I am not a developer and I am just speculating here…

    but this line
    $cptui_post_types = array( 'vid','post' );

    seems to only refer to custom post types? I want to show 1 custom post type (vid) along with regular wordpress posts.
    Maybe it needs a little tweaking?

    Below is the whole code I am using.

    Thanks for any input on this!

    // CPT UI - add custom post type to default wordpress taxonomies
    
    function my_cptui_add_post_types_to_archives( $query ) {
    	// We do not want unintended consequences.
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;    
    	}
    
    	if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'vid','post' );
    
    		$query->set(
    	  		'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
    Thread Starter Kyrian

    (@oloccina)

    Alright,
    I found another code snippet here https://premium.wpmudev.org/blog/add-custom-post-types-to-tags-and-categories-in-wordpress/ and this works to show custom post types along with regular wordpress posts in my Uncode theme.
    I post it here in case someone needs it

    
    function add_custom_types_to_tax( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
     
    // Get all your post types
    $post_types = get_post_types();
     
    $query->set( 'post_type', $post_types );
    return $query;
    }
    }
    add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    For what it’s worth, that last version just adds ALL of the post types, regardless of what/who is registering them.

    Only thing standing out for me with the variation I’ve provided is if possibly the array merge is failing. Plus it’s specifying “post” twice but I don’t that should affect things too much.

    Thread Starter Kyrian

    (@oloccina)

    Yep,
    I have slightly changed the code to specify only some post types (but I’ve posted the original code for other to use.

    Here is the version I am using

    // Add custom post type to default wordpress taxonomies
    
    function add_custom_types_to_tax( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
     
    $post_types = array( 'post', 'vid', 'meditazione' );
     
    $query->set( 'post_type', $post_types );
    return $query;
    }
    }
    add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

    I have no idea why this one works either, but it does.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    that’s the important part, it’s working.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Archive page only whops default posts type’ is closed to new replies.