Support » Plugin: Relevanssi - A Better Search » Filter custom post type from appearing in global WordPress search results

  • Resolved besweeet

    (@besweeet)


    I have a custom post type (Pods) that I need to display on a specific page as search results. I currently use Relevanssi and Search & Filter Pro for this.

    However, this custom post type is showing up in global WordPress searches when I don’t want it to.

    Is there a way to filter out the custom post type for global WordPress searches?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Yes. The best solution is probably to set a default post type setting that excludes the Pods post type:

    add_filter( 'relevanssi_modify_wp_query', 'rlv_default_post_type' );
    function rlv_default_post_type( $query ) {
    	if ( ! isset( $query->query_vars['post_types'] ) || 'any' === $query->query_vars['post_types'] ) {
    		$query->set( 'post_types', '-pods' );
    	}
    	return $query;
    }

    This will exclude the pods post type from searches, unless the post_types parameter is explicitly set to pods.

    Thread Starter besweeet

    (@besweeet)

    Thanks for the function. When I add it, WordPress global search has no results, regardless of the query.

    In my case, the name of the custom post type is ecp-qa-entry. If I replace $query->set( 'post_types', '-pods' ); with $query->set( 'post_types', '-ecp-qa-entry' );, there are still no results. If I remove the - in front of ecp-qa-entry, only posts in that post type are displayed as search results.

    Plugin Author Mikko Saari

    (@msaari)

    Try changing -ecp-qa-entry to a list of post types you want to include. If you want posts and pages, for example, use array( 'post', 'page' ) instead of -ecp-qa-entry.

    Thread Starter besweeet

    (@besweeet)

    Using an array worked! Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Filter custom post type from appearing in global WordPress search results’ is closed to new replies.