Is it possible to only show a certain category for the blog?

Hi, I’m using a blog for the News & Media page on a site and at the minute it’s showing the all the categories, ‘News & Media’ and ‘Uncategorized’. Is it possible to only show the ‘News & Media’ category? I’ve looked in the Appearance > Customise settings but can’t find anything, unless I’m missing it somehow!

I’ll put the URL in a secure note as it’s being built on a temporary link.

Thanks!

Hi @core365,

Please install and activate the child theme and login through FTP then edit the functions.php then add this code:

/*
* Exclude 
* ----------------------------
*/
function override_queries( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin()  ) {
      $query->set( 'cat', '-XX' );
    }
}

add_action( 'pre_get_posts', 'override_queries' );

Please replace XX in the code above to the ID of the uncategorized category.

Here is how you can find the ID:

Hope this helps.

Hi, thanks for that, however I don’t know what I’ve done wrong as it’s still showing all of the categories on the News & Media page. The ID of the uncategorized category is 1 which I replaced the ‘XX’ with, but unfortunately it’s not working.

Thanks!

Hey @core365,

Please try:

/*
* Exclude uncategorized
* ----------------------------
*/
function override_queries( $query ) {
  if ( $query->is_home() && $query->is_main_query() && ! is_admin()  ) {
    $query->set( 'cat', '-1' );
  }
}

add_action( 'pre_get_posts', 'override_queries' );

Hope this helps.

Hi, I tried to add that in but every time I went to save it, it kept coming up with an error message:

Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.

Here’s a screen grab for your reference:

Thanks!

Hi @core365,

I have checked the code you have added based on the screenshot you have posted and nothing seems to be wrong with the code.

Please login through FTP then add the code to the functions.php file.

Hope this helps.

Yep, that’s sorted it, thanks!

Thanks for your time and help!

Glad to hear that. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.