Site logo

Archived topic

How to exclude a category from displaying on Blog page?

19 replies · Started by Francisco on October 18, 2018

Viewing posts 1–15 of 20

Hi.

I am using GP Premium 1.7.2.

I have 10 different post categories, but I want to publish only 8 of them in the blog page. The other 2 I want to publish in a widget. How can I control the categories I want to display in the Blog page?

Hi,

I tried to enter the function but it does not work for me. Could you please look into it?

This is the function i used.

add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-3,-5,-23');
}
return $query;
}

You need to replace this part: 'cat', '-3,-5,-23' with the actual name of the categories you want like to exclude. If there is only one category you want to exclude, then you just need to include one quotation.

Let me know if this makes sense :)

Yes, i know.

The name of the category is Nyheter. And this is how my function looks like.

add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-nyheter');
}
return $query;
}

Try:

add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
      if ( $query->is_home ) {
            $query->set('Nyheter');
      }
      return $query;
}

It does not work either.. Sorry.

Sorry my mistake.

You need to find the category ID, then replace the -3 below:

add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
      if ( $query->is_home ) {
            $query->set('cat', '-3');
      }
      return $query;
}

Thank you!

Now it works. :D

No problem :)

I’m trying to make this work, but… Where do I need to include the function? Within the file for the Blog page or is it enough to put in the personalized CSS? Sorry for the basic question. (I suppose I need to modify the file, but before doing something like that, I prefer to ask.)

Many thanks.

Note: maybe it doesn’t work because it was 2018 and something has changed.

Thanks, David!
Have you ever considered to change your name in Flash? It’s always surprising how fast you guys are answering... Touché! (And happy with GP Pro!) Never felt so happy with one of my websites.

haha - we try our best.
Glad to hear you're happy with GP + GPP - feedback is much appreciated.

Hi great guys,

I used the "Code Snippets" plug-in it's suggested on the page you linked, David.
Many thanks, it worked great.

But now I have a strange behaviour in the blog (I don't know when it started): I cannot see the last posts. And I don't understand where is the fault. I just added this snippet to exclude 3 categories:

add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-141,-142,-143');
}
return $query;
}

Here is the blog page:
https://s3nzanom3.com/blog-s3nzanom3/

As you can see, I have 3 newer posts after the last showed and they are not in the excluded categories. What's wrong? Do you have any idea?

Note: I posted the same problem on the "WP Show Posts" support page, sorry. I mixed two different errors there. I'm going to comment on that, because this issue seems more related to (my) GP configuration and it's not related to show posts, as it's the main blog page.

This archived topic is closed to new replies.