0

I want to exclude a specific category from the loop, here's what I tried:

Original code:

    // auto posts by cat
    if( $bawmrp_options['auto_posts'] == 'cat' || $bawmrp_options['auto_posts'] == 'both' ):
        $cat = get_the_category( $post->ID );
        $cat_ids = wp_list_pluck( $cat, 'term_id' );
        $args['category'] = implode( ',', $cat_ids );
    endif;

My try:

    // auto posts by cat
    if( $bawmrp_options['auto_posts'] == 'cat' || $bawmrp_options['auto_posts'] == 'both' ):
        $cat = get_the_category( $post->ID );
        if ($cat->cat_ID != '68') :
            $cat_ids = wp_list_pluck( $cat, 'term_id' );
        endif;
        $args['category'] = implode( ',', $cat_ids );
    endif;

In my code I tried to exclude the category 68, but actually the best thing would be if the category has children, then it don't enter in the loop, only it's children

1
  • In your try $cat is an array so $cat->cat_ID will not work. Was there no error encountered in the above code> Commented May 17, 2014 at 2:22

1 Answer 1

1
$cat_ids = wp_list_pluck( $cat, 'term_id' );

Once you get the category ids as you have mentioned above, you can search for the key having the category id 68 as

$key = array_search('68', $cat_ids);

Once you get the key, you can unset/remove that from the array as

unset($cat_ids[$key]);

Once that is removed, you can use the implode function the way you are currently using.

1
  • Thanks for answering, I don't know if I'm doing anything wrong, but it's still not working, please take a look: pastebin.com/gwpydNx1 Commented May 20, 2014 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.