carasmo
1/29/2017 - 5:58 PM

Exclude Categories from the post_meta output

Exclude Categories from the post_meta output

<?php
//don't add

/**
 *
 * Exclude Terms by ID from the get_the_category_list(); (WordPress core)
 * Ref: http://stackoverflow.com/a/34817401/1004312
 * I add a post to two categories with the ids (examples) 200 and 266 
 * but I only want 200 in the post_meta shortcode in Genesis
 * which uses the get_the_category_list(); (WordPress core) which has no filter
 * But that uses uses get_the_terms(); (WordPress core) which does have a filter
 * The problems this leads to is that if 266 is a main category for some other post, it won't show.
 *
 * This is not recommended, instead just assign posts to a single category and use tags
 *
 */
function yourprefix_exclude_terms( $terms ) {

	// put term ids here comma separated
    $exclude_terms = array( 266 );
    
    if (!empty( $terms ) && is_array( $terms ) ) {
        foreach  ($terms as $key => $term ) {
            if (in_array( $term->term_id, $exclude_terms ) ) {
                unset( $terms[ $key ] );
            }
        }
    }

    return $terms;
}
add_filter( 'get_the_terms', 'yourprefix_exclude_terms' );