Community Forums › Forums › Archived Forums › General Discussion › Removing blog categories from Blog page
Tagged: blog page, blog template
- This topic has 8 replies, 3 voices, and was last updated 3 years, 6 months ago by
Terry.
-
AuthorPosts
-
November 19, 2019 at 6:44 am #494858
Colinz
ParticipantHi,
I'm having issues excluding blogs with specific categories from the blog page.
I have tried:
add_action( 'pre_get_posts', 'be_exclude_category_from_blog' );
/**
* Exclude Category from Blog
*
* @author Bill Erickson
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @param object $query data
*
*/
function be_exclude_category_from_blog( $query ) {if( $query->is_main_query() && $query->is_page('9') ) {
$query->set( 'cat', '-50','-61','-62' );
}
}and I have tried (on the blog page):
Custom Field:
Name: query_args Value: cat=-50,-61,-62A moderator suggested:
// Exclude categories from blog archive.
add_action( 'pre_get_posts', 'exclude_categories_blog_archive' );
function exclude_categories_blog_archive( $query ) {
if( $query->is_main_query() ) {
$query->set( 'cat', '-61,-62,-50' );
}
}But that removes them from the Posts> All Posts as well as the blog page.
Help gratefully received.
Colinz
November 19, 2019 at 1:44 pm #494871Terry
MemberRight, the
$query->is_main_query()
filters those specified categories from the main query which is what you're displaying on the posts page (Blog).It looks like you have a number of post listings on your site.
Are you trying to filter ob the blog and other locations? Add a test for that page:
`// Exclude categories from blog archive.
add_action( 'pre_get_posts', 'exclude_categories_blog_archive' );
function exclude_categories_blog_archive( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$query->set( 'cat', '-61,-62,-50' );
}elseif ( is_page(whatever_the_page_id)) {
// for single category 5
$query->set( 'cat', ‘-5’ );// for multiple categories 3 & 4
//$query->set( 'cat', '-3, -4' );
}`
November 19, 2019 at 1:52 pm #494874Terry
MemberThe
! is_admin() &&
tests for admin to distinguish main query.November 19, 2019 at 2:01 pm #494875Terry
MemberLastly, you don't need both conditions but it helps me for understandability/readability
$query->is_main_query() && $query->is_home()
November 20, 2019 at 5:59 am #494895Brad Dalton
ParticipantAnother method is to use custom fields like this
You can also use the genesis_pre_get_option_blog_cat filter hook.
November 20, 2019 at 7:32 am #494899Colinz
ParticipantThank you both for taking the time to help me out. I now have it set up as I wanted.
November 20, 2019 at 9:18 am #494901Terry
Member@braddalton To share my head banging the other day... I'm possibly not using the query_args correctly but now (WP 5.3, Genesis 3.2.1, Genesis Sample 3.2.1) the custom fields approach works for a page that is populated with posts (say with AB Posts and Pages Grid block) but not on a page titled "Blog" that is set as Posts Page.
Tried both query_args 'cat=-5'(no quotes in field) as well as 'category__not_in'(no quotes in field) on the Blog page, neither successful. However, action hook (as above) was successful.
November 20, 2019 at 9:29 am #494902Brad Dalton
ParticipantNovember 20, 2019 at 10:15 am #494903Terry
MemberThanks Brad! 🙂
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.