Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created December 13, 2016 15:59
Show Gist options
  • Select an option

  • Save bdeleasa/3f5c9baede7fa4972bd3999d3df35d2f to your computer and use it in GitHub Desktop.

Select an option

Save bdeleasa/3f5c9baede7fa4972bd3999d3df35d2f to your computer and use it in GitHub Desktop.
Wordpress plugin that filters the Wordpress search results to show only posts, no other post types.
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @since 1.0.0
* @package search-filter-only-posts
*
* @wordpress-plugin
* Plugin Name: Search Filter - Only Posts
* Plugin URI: https://chooserethink.com
* Description: Filters the Wordpress search results to show only posts, no other post types.
* Version: 1.0.0
* Author: Brianna Deleasa @ LoudMouse
* Author URI: http://loudmouse.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: search-filter-only-posts
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_filter( 'pre_get_posts', 'sfop_filter_search_to_only_posts' );
/**
* Filters the front end search form so it only searches through posts.
*
* @since 1.0.0
*
* @param $query
* @return mixed
*/
function sfop_filter_search_to_only_posts($query) {
if ( $query->is_search && ! $query->is_admin() ) {
$query->set( 'post_type', 'post' );
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment