Support » Developing with WordPress » Exclude Category using Child Theme

  • I feel like I’m doing everything with best practices and it’s not working.

    I created a child theme, so as not to affect the original functions.php.

    I know my child theme functions page is being “read” cuz when I have a typo, it gives me an error message.

    Here’s my exclude function, copied & pasted directly from the codex page.

    *******************************************************************

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

    *******************************************************************

    Yes, I’ve double-checked that the category numbers are correct. Yes, I’ve tried the opposite function, to display just one category, also didn’t work. (i.e. all categories continue to display.)

    Why isn’t it excluding those two categories (advertisers, testimonials)?

    • This topic was modified 2 years, 2 months ago by Steve Stern.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    If this is the only thing the child does, you could using this plugin with the parent instead: https://wordpress.org/plugins/ultimate-category-excluder/

    I think the code should be

    $query->set( ‘cat’, array(-17,-18 );

    Moderator bcworkz

    (@bcworkz)

    It may be related to how your site is setup and/or how your theme manages the home page content. If you have your site set to use a static front page, your query adjustment will only be applied to the blogs page, not the front “home” page.

    If your theme is running some other sort of query to generate home page content, so that the query used is not the “main” query, your code is ignored. This is more common than you may realize, even though such themes are following poor practice. If the theme uses an SQL query to get posts, the pre_get_posts action will not even fire. Your code needs to work with your theme. Code pulled from the Codex is typical, but not necessarily applicable.

    It’s also possible some other pre_get_posts hook is overriding or negating your code. These can be difficult to track down. First try removing the conditionals from your code. It will be applied for queries where it’s undesirable, but it will at least confirm the set value is appropriate and that pre_get_posts fires.

    Then try the conditional is_front_page() instead of is_home() — without the main query check for now. If that works, try adding the main query check back in. If any of these steps fail to work, we’ll need to look at how the theme is generating the query.

    Passing cat arguments as a string value is acceptable, but I would advise using array syntax exclusively. There are some query vars where only array syntax will work. Mixing array and string syntax is a bad idea IMO. The proper array syntax is
    $query->set('category__not_in', array( 17, 18,));

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude Category using Child Theme’ is closed to new replies.