• Resolved rinart73

    (@rinart73)


    For example I have a custom post type “testimonials” with rewrite slug “reviews” and a custom taxonomy “testimonials_category” with rewrite slug “reviews-cat”.
    example.com/reviews/ shows my testimonials archive.
    example.com/reviews-cat/some-term/ shows testimonials in specific category.
    Hovewer if I enable “exclude from search” for the custom post type, taxonomy archives stop working. They still have the SEO title and breadcrumbs (so they’re properly detected) but they show that no posts found.

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

    (@tw2113)

    The BenchPresser

    For whatever reason, WordPress core decided to remove post types from the term archives when exclude from search is set to true. I don’t understand why either, but it’s how it is.

    We actually added notes about this exact detail in our FAQs and around the “Exclude from search” field for post types, in the 1.11.0 release of CPTUI.

    That said, the recommended workaround for this is to leave exclude_from_search set to false for the post type, but then also use the pre_get_posts filter to remove the post type from consideration.

    Borrowing a snippet from https://wp-mix.com/wordpress-exclude-custom-post-type-search/, this could be achieved with the bit below, though you’ll want to change it up to include any custom post types you do want considered still. So if you have more than just reviews, you’d want to replace book with that post type’s slug. The list from array('post', 'page', 'book') are the ones you STILL want to search.

    function shapeSpace_filter_search($query) {
    	if (!$query->is_admin && $query->is_search) {
    		// Set the post types you DO want to still search through.
    		$query->set('post_type', array('post', 'page', 'book'));
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'shapeSpace_filter_search');
    Thread Starter rinart73

    (@rinart73)

    Yeah that’s a weird decision from WordPress. Got it thank you.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    New dedicated documentation article for anyone coming by in the future

    https://docs.pluginize.com/article/exclude-post-type-from-search-but-retain-taxonomy-archive-listing/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude custom post type from search but keep custom taxonomy functional’ is closed to new replies.