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...

MemberPress Membership login for WordPress Review
Content restriction

MemberPress for WordPress Review

MemberPress for WordPress is a powerful membership plugin that makes it easy to create and manage memberships on any WordPress website. It allows you to

Read More »
Pre-built websites in BeTheme
Themes

Betheme for WordPress Review

Betheme is a WordPress theme that has been described as one of the most powerful and versatile themes available for WordPress. In this Betheme for

Read More »

Comment

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