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

  • In your try $cat is an array so $cat->cat_ID will not work. Was there no error encountered in the above code> – Chittaranjan May 17 '14 at 2:22
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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