Category Archives: Functions

Hide pages in WordPress search based on a template

Here is some code that allows you to hide pages from WordPress search based on the pages using a specific template.

Let’s say you had a Members Only section that was accessible by logged in members to your site. But what happens when this locked down content appears in your site’s search results? Below is a way you can hide these pages from appearing in the search results.

Step 1)
Create a template or clone / copy an existing template and call it something like Exclude Search Template (exclude.php) or Members Template (members.php). Place (e.g., FTP) your template into your child theme’s directory.

Now place the below code into your child theme’s function file. Makes sure the name of the template appears in the code below.

function exclude_page_templates_from_search($query) {
global $wp_the_query;
if ( ($wp_the_query === $query) && (is_search()) && ( ! is_admin()) ) {
$query->set(
'meta_query',
array(
array(
'key' => '_wp_page_template',
'value' => 'members.php',
'compare' => '!='
)
)
);
}
}
add_filter('pre_get_posts','exclude_page_templates_from_search');

That’s it, your done. Now if you find members content in your search results, you can click on these pages and change them to use the Members template and they will not appear in the search results again.

Only show the WordPress toolbar to Admins

If you wish to disable the WordPress toolbar for all users except Admins, then place this small snippet of code in your theme’s function file.

// Remove Toolbar for users except for Admins
if (!current_user_can('administrator')) :
  show_admin_bar(false);
endif;

Why would you want to disable the toolbar in the first place? Well for one, if you run a BuddyPress site and most members on your website are BBPress forum participants, then the toolbar is overkill as you already have your user profile on the front-end of the website. In fact any kind of members plugin that has user profiles on the front-end of the site are better without the toolbar.

However, retaining the toolbar for Admins is a good idea if you decide to disable the toolbar for other roles. The reason is it gives quicker access to the Admin area without having to append /wp-admin/ to the URL to enter the CMS. It is also handy as you can directly edit pages when on the website as the ‘Edit’ link on the toolbar is available. Otherwise you have to enter the CMS and go to Pages, then search for the page. More often than not, you notice that pages need a correction or edit when you are reading the page from the website, so you want to be able to fix it easily right there and then.

If you want other user roles to have access to the toolbar like Editors, then try the code below and edit depending on what user roles you want to show the toolbar too.

add_action( 'after_setup_theme', 'remove_admin_bar' );
function remove_admin_bar() {
    if( ! current_user_can( 'administrator' ) || ! current_user_can( 'editor' ) || ! current_user_can( 'author' ) ) {
        show_admin_bar(false);
    }
}

There are of course a number of plugins that can do this for you, but I prefer to add code to the function.php file in your theme as it is much lighter than loading a plugin. Often plugins have a number of features that you may not use, but you have to load that code anyway.