Created
December 19, 2019 18:52
-
-
Save bdeleasa/f50d1d4db4a9cbef8556aa215fa630c4 to your computer and use it in GitHub Desktop.
Limits Wordpress search results to only the 'post' post type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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