Skip to content

Instantly share code, notes, and snippets.

@itzmekhokan
Last active March 26, 2019 15:56
Show Gist options
  • Save itzmekhokan/4a004e522c404cfc9cb6546583cd67cb to your computer and use it in GitHub Desktop.
Save itzmekhokan/4a004e522c404cfc9cb6546583cd67cb to your computer and use it in GitHub Desktop.
WordPress Custom search Include All Custom Post Types
<?php
function my_custom_search($query) {
// Check to verify it's search page
if( is_search( ) {
$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
$searchable_types = array();
// Add available post types
if( $post_types ) {
foreach( $post_types as $type) {
$searchable_types[] = $type->name;
}
}
$query->set( 'post_type', $searchable_types );
}
return $query;
}
add_action( 'pre_get_posts', 'my_custom_search' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment