Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created December 19, 2019 18:52
Show Gist options
  • Save bdeleasa/f50d1d4db4a9cbef8556aa215fa630c4 to your computer and use it in GitHub Desktop.
Save bdeleasa/f50d1d4db4a9cbef8556aa215fa630c4 to your computer and use it in GitHub Desktop.
Limits Wordpress search results to only the 'post' post type.
<?php
/**
* @file wp-limit-search-results.php
*
* Limits the website search results to just searching posts.
*
* @package wplsr
* @since 1.0.0
*/
add_filter( 'pre_get_posts', 'wplsr_adjust_search_query' );
/**
* Function that limits search queries to only 'post' post types.
*
* @since 1.0.0
*
* @param $query WP_Query object
* @return object
*/
function wplsr_adjust_search_query($query) {
if ( $query->is_main_query() && $query->is_search && ! is_admin() ) {
$query->set( 'post_type', array( 'post' ) );
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment