php
How to Hide a WordPress Post from Public Users with a Specific Category ID

How to Hide a WordPress Post from Public Users with a Specific Category ID

Do you have a post on your WordPress site that you want to hide from public users, but still show to admins? Perhaps you have a sensitive announcement or a draft that you’re not ready to publish yet. Whatever the reason, you can use the code below to hide a post with a specific category ID from public users while still showing it to admins.

Code:

// Hiding post from public users if the category is 141 which is Push Notification.
function exclude_push_notification_blog( $query ) {
    // current_user_can( 'manage_options' ) means if its admin
    if ( ( $query->is_main_query() || $query->is_home() ) && !current_user_can( 'manage_options' )) {
        $query->set( 'cat', '-141' );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'exclude_push_notification_blog' );

In this code, we use the pre_get_posts filter to modify the WordPress query and exclude any posts with the category ID of 141, which is Push Notification. The current_user_can( 'manage_options' ) function checks if the current user is an admin, and if they are, the post will still be displayed.

Explanation:

  1. $query->is_main_query(): This is a method in WordPress that checks whether the current query is the main query or not. The main query is the query that WordPress runs to generate the main content of the page, such as posts on an archive page or the main post on a single post page. This part of the code checks if the current query is the main query.
  2. $query->is_home(): This is a method in WordPress that checks whether the current query is the home page query or not. This part of the code checks if the current query is the home page query.
  3. &&: This is a logical operator that combines two conditions, so that both conditions must be true for the code inside the if statement to execute.
  4. !current_user_can( 'manage_options' ): This part of the code checks if the current user is not an administrator. The current_user_can function checks if the current user has a specific capability, in this case, the manage_options capability, which is typically only available to administrators.
  5. $query->set( 'cat', '-141' );: This part of the code modifies the query to exclude posts from category 141. The set method sets a query variable to a specific value, in this case, it sets the cat (category) parameter to -141, which excludes posts from category 141 from the query.

This is a simple and effective way to hide sensitive content from public users while still allowing admins to access it. Keep in mind that this only hides the post from public-facing pages, and the post can still be accessed directly if someone knows the URL.

functions.php file

The functions.php file is a common file used in WordPress themes and plugins to add custom PHP functions and modify the behavior of WordPress.

The functions.php file is located in the wp-content/themes/your-theme-name/ directory for themes or wp-content/plugins/your-plugin-name/ directory for plugins.

Here’s how to find the functions.php file:

  1. Log in to your WordPress website’s admin dashboard.
  2. In the left sidebar, hover over “Appearance” for themes or “Plugins” for plugins and click on “Editor”.
  3. In the right-hand side of the editor, you should see a list of files. Look for functions.php.
  4. If you can’t see functions.php, click on the dropdown labeled “Select theme to edit:” to select your theme or plugin from the list of available themes or plugins.
  5. Once you have found functions.php, you can edit it by clicking on the file name.

Note that editing the functions.php file can have significant consequences on the behavior of your WordPress site. It is recommended that you make a backup of the file before making any changes and only modify the file if you know what you’re doing.

Leave a Reply