Support » Plugin: Custom Post Type UI » How to add custom posts to Categories, Archives and Search

  • Resolved onlinerockers

    (@onlinerockers)



    I have created few custom post types like offers and reviews. I am using the post’s taxonomies for my custom posts too.

    But, when I check these categories/tags/search results, my custom posts are not appearing.

    How to make my custom posts appear on category pages, tags pages and search results?

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    Thank you 🙂 It works now.

    Hi, I reviewed and integrated the code in these 2 posts:

    http://docs.pluginize.com/article/23-post-type-posts-in-search-results
    http://docs.pluginize.com/article/17-post-types-in-category-tag-archives

    But I have not been successful in getting the CPTs to appear in searches or categories.

    This is the code I added to the functions.php file following the instructions provided:

    // To display CPTUI posts in archives
    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() && empty( $query->query_vars['suppress_filters'] ) ) {
    
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( '2018_artists' );
    
    		$query->set(
    	  		'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
    
    // To display CPTUI posts in search
    function my_cptui_add_post_type_to_search( $query ) {
    	if ( $query->is_search() ) {
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( '2018_artists' );
    
    		$query->set(
    			'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );

    Searches and categories return: SORRY, NO POSTS FOUND

    Any idea what the problem might be?

    Plugin Author Michael Beckwith

    (@tw2113)

    @dhsllc not sure what’s going on with your case, as both snippets look solid to me, and when I dropped them into my local dev, they both worked.

    What file/location are you saving the snippets to?

    I’m using Elegant Themes, theme Extra and created the child theme with Child Theme Configurator by Lilaea Media which added the functions.php file under the child theme.

    I’ve added other custom code on there which is working. What else could be conflicting?

    Plugin Author Michael Beckwith

    (@tw2113)

    Really hard for me to say what’s going on without having access to things, truth be told. I wish I had a better answer for you at the moment. It sounds like it’s in a place that it should be loading and running fine.

    I’ll be glad to share the site link or set you up with access if it helps. I’ve tried a few more things but I just can’t get this to work. I could definitely use some help. Thank you.

    Plugin Author Michael Beckwith

    (@tw2113)

    Just to be certain and double check the fine details. Have you made sure to add category support for the post type in question, and made sure it’s not set to not be in searches? There’s two possible spots that this may be affected by: “Exclude From Search” and “Publicly Queryable”

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to add custom posts to Categories, Archives and Search’ is closed to new replies.