Archived topic
limiting a category
9 replies · Started by Streater on July 7, 2021
So I got the blog/Newsroom page how I wanted it after help from David:
https://generatepress.com/forums/topic/changing-post-thumbnail-problem/
BUT I needed a whole other Blog page for Career postings so I downloaded WP Show Posts... BUT the Newsroom images wouldn't display if I sized them with heights and widths. Careers didn't need images so WP Show Posts works fine.
So I thought I'd revert back to the default for the blog/Newsroom and use WP Show Posts for Careers since I got the setup I wanted after David's help. BTW Newsroom and Careers are separate Categories and I wanted separate Blog pages for each.
THE PROBLEM: Careers categories show up on the Newsroom Blog. So I found this:
https://generatepress.com/forums/topic/exclude-category-from-loop/#post-111543
BUT this code is if the homepage was a blog (mine is separate page) so I thought I'd modify it:
<?php
add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->home_url( '/newsroom/' ) ) {
$query->set('cat', '-1');
}
return $query;
}
?>
This doesn't seem to work... any ideas?
Hi Streater,
On your code, you should change the category ID.
If you want to exclude "Careers", you must add its category ID instead.
Say, for example, "Careers" category has an id number of 33.
Here's how the code should be -
<?php
add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->home_url( '/newsroom/' ) ) {
$query->set('cat', '-33');
}
return $query;
}
?>
I've basically changed -1 to -33 to match the category ID of the category I want to exclude.
Careers category ID is 6 so I changed it to -6... still not working. Any ideas?
Thanks
Do you have any CDN or deep cache that caches the pages?
If yes, can you try clearing that after applying the change on the code?
No I don't... it's like a staging site
/newsroom is th blog index page so we can try using $query->is_home() in the condition.
Can you try this?
<?php
add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home() ) {
$query->set('cat', '-6');
}
return $query;
}
?>
For some reason this crashes the site.
Isn't that for removing categories on the homepage? I want to remove it on the newsroom/blog index page
For some reason this crashes the site.
Strange. The code is pretty straightforward. Can you share how you're adding it? Perhaps provide temporary backend access so we can help in checking? (you can provide the details on the private information text field)
Isn’t that for removing categories on the homepage? I want to remove it on the newsroom/blog index page
is_front_page() is for the home page. is_home() is for the blog index page, which in your site's case is the /newsroom page.
https://share.getcloudapp.com/d5uA50bG
I think it crashed because I still had the other function still... I replaced it with yours and it works! Thanks for your help!
Ah that makes sense. Nice catch. Glad it works for you now. No problem. :D