Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Exclude Post Types From Search

This snippet allows you to remove a post type, like pages, for instance, from the search results without removing any others (by default WordPress searches posts, pages, and attachments). This will also preserve any custom post types you might have that are searchable (like those from other plugins).

<?php
/**
* Modify query to remove a post type from search results, but keep all others
*
* @author Joshua David Nelson, [email protected]
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2+
*/
add_action( 'pre_get_posts', 'jdn_modify_query' );
function jdn_modify_query( $query ) {
// First, make sure this isn't the admin and is the main query, otherwise bail
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 'page'
$post_type_to_remove = 'page';
// 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 );
}
}
}
view raw remove-post-type-from-search-results.php hosted with ❤ by GitHub
<?php
/**
* Modify query to remove a post type from search results, but keep all others
*
* @author Joshua David Nelson, [email protected]
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2+
*/
add_action( 'pre_get_posts', 'jdn_modify_query' );
function jdn_modify_query( $query ) {
// First, make sure this isn't the admin and is the main query, otherwise bail
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 ) );
// Make sure you got the proper results, otherwise bail.
if ( ! is_array( $in_search_post_types ) ) {
return;
}
// The post types you're removing, in this example 'page' and 'post'
$post_types_to_remove = array( 'post', 'page' );
// loop through each one and remove it from the searchable post types, if it's in there.
foreach ( $post_types_to_remove as $post_type ) {
// Confirm if the post type is in the results
if( in_array( $post_type, $in_search_post_types ) ) {
// Remove the post type from the array
unset( $in_search_post_types[ $post_type ] );
}
}
// set the query to the remaining searchable post types
$query->set( 'post_type', $in_search_post_types );
}
}
view raw remove-post-types-from-search-results.php hosted with ❤ by GitHub

Source: https://gist.github.com/joshuadavidnelson/127e51326c04367036da

Tags: custom post type, search

Previous Code:
Get Post Types By Taxonomy
Back to the Code Snippets
Next Code:
Remove Genesis Inpost Layout Box on Posts
  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact