WordPress don't want to show hide posts PHP

WordPress: Hide Specific Posts with PHP

Explains how to hide posts in specific categories in WordPress or display them through links using PHP implementation.

Shou Arisaka
2 min read
Nov 13, 2025

Explains how to hide posts in specific categories in WordPress or display them through links using PHP implementation.

My blog has articles dealing with criticism and deep topics about daily life. I consider these articles not suitable for public release.

How to Hide Posts and Countermeasures

The following shows how to hide posts in specific categories in WordPress or display them through links.

Disable Categories with Query Parameters

Add the following code to functions.php:

function exclude_category_bundle(){

  function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'cat', '-4,-3' ); // category: black and pink
    }
  }
  add_action( 'pre_get_posts', 'exclude_category' );
}

if (isset($_GET['black'])) {
    if ($_GET['black'] == "true") {
        // Display posts in specific category
    } else {
        exclude_category_bundle();
    }
} else {
    exclude_category_bundle();
}

Add a link to the navigation menu or header menu to enable displaying hidden posts.

<?php if (is_home()) {
    echo '<a title="Display posts that are hidden by default. Please view at your own discretion." style="position: absolute; right: 8em;" onclick="window.location = document.location.href + \'&black=true\'">Display hidden posts</a>';
}
?>

This allows you to show/hide posts in specific categories using query parameters.

Additional Information

  • WordPress: How to nofollow/noindex all posts in specific categories
  • WordPress PHP: How to warn on first access to posts in specific categories

This shows how to control and manage the display of posts in specific categories in WordPress.

Share this article

Shou Arisaka Nov 13, 2025

๐Ÿ”— Copy Links