Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Exclude custom post type category from homepage

$
0
0

Replies: 0

Hi there,

I am trying to exclude a custom post_type category from displaying on the homepage. I am half way there…

If I remove the <if> statements in the following code it works perfectly but I need them otherwise the function is applied to all pages in the site, not just the home page.

I have a static front page configured.

This is the code I have been trying to use:

add_action('pre_get_posts','exclude_cat');
function exclude_cat( $query ) {
	if ( !is_admin() && $query->is_main_query() ) {
		$tax_query = $query->get( 'tax_query' ) ?: array();
		$tax_query[] = array(
			'taxonomy' => 'st_category',
			'field'    => 'id',
			'terms'    => array( 115 ),
			'operator' => 'NOT IN',
		);
		$query->set( 'tax_query', $tax_query );
	}
	return $query;
}

I have also tried the following <if> statements with no success:

if( $query->is_page( array( 113, 'portfolio', 'Portfolio' ) ) ) {

if( $query->is_home() ) {

if( $query->is_front_page() ) {

What is going on?

Please help if you can – thank you so much!


Viewing all articles
Browse latest Browse all 59525

Trending Articles