WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Child Category Exclude (5 posts)

  1. joshrodgers
    Member
    Posted 1 year ago #

    I am trying to exclude all child categories of a specific category, I found: http://wordpress.org/support/topic/exluding-post-categories-from-query_posts-by-nameslug-with-get_cat_id, which excludes the category, but not the category and it's child categories. How do I tweak this code so that it works for both the category and the child category?

    My current code looks like: http://pastebin.com/0n3E62nB

    Thanks,
    Josh

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    $args = array(
        'child_of' => 5 (or whatever the id is)
    );
    
    $baz = get_categories( $args );
    
    foreach($baz as $foo)
    {
    	$exclude[] = $foo->term_id;
    }
    $query = new WP_Query( array( 'category__not_in' => $exclude ) );
    
    while($query->have_posts()) { $query->the_post(); the_content(); }
  3. joshrodgers
    Member
    Posted 1 year ago #

    Andrew,

    This looks awesome, but after I added the code, my website disappeared...I removed the code and it re-appeared, I'm thinking due to something in the code...any ideas?

    Thanks,
    Josh

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    I copy/pasted it from my test environment after getting it to work. What did you replace the "5" with in line 2?

  5. joshrodgers
    Member
    Posted 1 year ago #

    I decided to go in another direction with this website...I'm doing the same thing, just doing it differently, making this question no longer valid.

    Thanks,
    Josh

Topic Closed

This topic has been closed to new replies.

About this Topic