Search
Close this search box.

How to disable public search in WordPress

To disable public search in WordPress, you can use the pre_get_posts action in your theme’s functions.php file. This action allows you to modify the main query object before it is executed.

Here’s an example of how you can use the pre_get_posts action to disable public search in WordPress:

// How to change disable search in WordPress
// More snippets at wpunplugged.com 

add_action( 'pre_get_posts', 'my_disable_public_search' );
function my_disable_public_search( $query ) {
  if ( $query->is_search ) {
    $query->set( 'post_type', 'private' );
  }
}

This code will modify the main query object to only search for posts of the private post type when a search is performed. Since public search is disabled, this will effectively disable public search on your WordPress site.

Note that this code will only disable public search on the front-end of your site. Administrators will still be able to search all content from the WordPress admin dashboard.

Share this post

You might also like...

Forum

bbPress for WordPress Review

bbPress is a great plugin for WordPress that offers a powerful forum solution for WordPress websites. It is well-supported, easy to use, and provides great

Read More »
WP Rocket for WordPress Review
Cache

WP Rocket for WordPress Review

WP Rocket is a popular WordPress plugin that offers comprehensive caching and optimization solutions for WordPress websites. It is designed to help improve website performance,

Read More »

Comment

Your email address will not be published. Required fields are marked *