I want to exclude XTheme portfolios from search

Hello,

I understand that portfolios in XTheme are taxonomically just special pages, but I have not been able to figure out how to hide all portfolios from search results for everyone but admin.

I already have a child theme set up and am glad to use PHP.

Thanks for any insight.

Hi There @sylvianibley

Thanks for writing in! First of all, please note that custom development is outside our scope of the support and we only provide a guideline for specific customizations.

Since you have a child theme already setup, try adding the following code into your child theme’s functions.php file.

// Remove Portfolios from Search Results
add_action( 'pre_get_posts', 'themeco_exclude_portfolio_from_search' );
function themeco_exclude_portfolio_from_search( $query ) {
 
 // First, make sure this isn't the admin 
 if( is_admin() || ! $query->is_main_query() )
 return;
 
 // If this is a search result query
 if( $query->is_search() ) {
 // Gather all searchable post types
 $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
 // The post type you're removing, in this example 'portfolio'
 $post_type_to_remove = 'x-portfolio';
 // Make sure you got the proper results, and that your post type is in the results
 if( is_array( $in_search_post_types ) && in_array( $post_type_to_remove, $in_search_post_types ) ) {
 // Remove the post type from the array
 unset( $in_search_post_types[ $post_type_to_remove ] );
 // set the query to the remaining searchable post types
 $query->set( 'post_type', $in_search_post_types );
 }
 }
}

Thanks!

Works great, thanks so much for your amazing speed.

You’re most welcome @sylvianibley! :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.