How to not display category in Blog Feed

I’m attempting to have my “Webinars” category not appear in my blog feed or archives. How would I go about doing this?

Here is my blog feed: https://www.corridorcompany.com/2017

P.S. I’ve already tried using this piece of code in my functions.php

    function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-15' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );

This does not remove the Webinar posts from the blog feed.

Hey @santosfel5,

Thanks for writing in!
I tried the same code and it is working fine at my end, Can you please check the category ID?

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Would you mind providing us with wp-admin login credentials? so we can take a closer look.

Regards

Sure, the login credentials to my site is below:

Please keep in mind that when you go to https://www.corridorcompany.com/2017 you will still see the Webinars category blog posts. The webinar category is 15.

Hey There,

Unfortunately, I do not have permission to access functions.php file. I am unable to access WordPress theme editor(https://www.corridorcompany.com/wp-admin/theme-editor.php).

Can you please check if category id is set to 15? The code should be “$query->set( ‘cat’, ‘-15’ );”.

Regards

To answer your question. Yes, I have set the category to -15.

My functions.php code is below

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to X in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );


// Additional Functions
// =============================================================================



/**
 * Restrict native search widgets to the 'post' post type
 */
add_filter( 'widget_title', function( $title, $instance, $id_base )
{
    // Target the search base
    if( 'search' === $id_base )
        add_filter( 'get_search_form', 'wpse_post_type_restriction' );
    return $title;
}, 10, 3 );

function wpse_post_type_restriction( $html )
{
    // Only run once
    remove_filter( current_filter(), __FUNCTION__ );

    // Inject hidden post_type value
    return str_replace( 
        '</form>', 
        '<input type="hidden" name="post_type" value="post" /></form>',
        $html 
    );
}  


function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-15' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );

Hello There,

Thanks for updating in! The code is working partially. That is because is_home refers to your blog index. We need to add another condition that will include the archive pages as well. In order exclude Webinars category in the blog index and any archive pages, you can make use of this code instead:

function exclude_category( $query ) {
	if ( ( $query->is_home() || $query->is_archive() ) && $query->is_main_query() ) {
		$query->set( 'cat', '-15' );
	}
}
add_action( 'pre_get_posts', 'exclude_category' );

If you need anything else we can help you with, please let us know.

Thank you! That worked.

Hey,

You are welcome!

I need to do the same thing, and I used the same code but my posts in that category ARE still showing… My category ID is 30, and below is the entire code for my functions.php, as well as the login details …

The only possibly difference I can think of is that I am using a child theme, so this is the x-child functions.php file…

Any ideas? Thx so much.

here are the login details

Hi @kirk74,

Thanks for writing around! I’ve removed your secure note from this post as this will be visible to the thread owner as well. if you need to share the credentials always make sure to create a new post and then use a secure note to share the credentials. I’d suggest you to please change the credentials as soon as possible for your own security and then create a new post regarding your issue then you can share the credentials in there. This way it will only be visible to you and our staff only.

Thank you for understanding!

OK thanks very much.

Any ideas though why that code didn’t remove the posts from that category for me?

For the moment I was able to remove from Recent Posts because I used the Exclude Category plugin … BUT - I have just noticed that I still see these posts I don;t want when using the next / previous buttons.

Thanks so much.

Hi there,

The best way to find out is that you open up a new thread and add your request and user info in a Secure Note. If you are the original poster of a thread you can use that functionality and the information will be between you and our support staff.

Thank you for your understanding.

No worries, thank you :slight_smile:

You’re welcome!