Archived topic
exclude categories on Blog.
10 replies · Started by Jusung on February 4, 2023
Hello. On my blog page, I want to exclude posts from certain categories.
I used this code as told.
add_action('pre_get_posts', 'exclude_category_posts');
function exclude_category_posts( $query ) {
if( $query->is_main_query() && !is_admin() && !is_singular() ) {
$query->set('cat', array( -9, -4, -11, -13, -12 ));
}
}
However, when I use this code, it works, but the querry loop pagenation function doesn't work.
So, the code affect the function of query loop pagenation when it is inherit query from template.
I need to use pagenation function, so I turned off the code above.
Is there other way to exclude some cetegories on blog?
Hi there,
is it breaking pagination ? Or is excluding so many posts that pagination is not required ?
It is breaking.
When I use the code to exclude categoreis on blog, it is breaking the function of pagenation when it is inherit query from template.
If i don't use inherit query from temlate, it works.
Therefore, right now, i turned off the code to exclude categoreis so that the pagenation of query loop works.
So, I need other way to exclude categories on blog..
Hi Jusung,
Which specific Queryy Loop is it affecting?
The main Query on the link you provided isn't created with a Query Loop.
Is that the correct page with the issue?
When I use the code to exlclude categories on blog,
on portfolio page, there is a query loop and I have made pagenation for that. Also, clicked inherit query from template.
on portfolio page, the pagenation of query loop doesn't work.
Now it is working since I have turned the code off.
How is it specifically breaking the pagination? Where do you want this code to run? Do you want it running on just the Blog page and not other pages like the Porfolio page?
Can you re-add the code so we can see what happens?
Simply the pagenation is gone.
I need the code to run only on blog.
I will add the code again not and you can see the pagenation is broken.
Can you replace this:
if( $query->is_main_query() && !is_admin() && !is_singular() ) {
with this:
if( $query->is_main_query() && !is_admin() && !is_singular() && is_home() ) {
in your code?
I will check it now.
Yes ! it works now!!
Thank you!!
You're welcome, Jusung!
