Include Attachments in WordPress Search Results
function my_attachments_search( $query ) { | |
if ( !is_search() ) | |
return $query; | |
$post_types = $query->get( 'post_type' ); | |
if ( ! $post_types || 'post' == $post_types ) | |
$post_types = array( 'post', 'attachment' ); | |
if ( is_array( $post_types ) ) | |
$post_types[] = 'attachment'; | |
$query->set( 'post_type', $post_types ); | |
return $query; | |
} | |
// hook our function to the filter | |
add_filter( 'pre_get_posts', 'my_attachments_search' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment