• Custom post types do not use the standard categories or tags as normal posts, but the capability is there to create a custom taxonomy – a custom set of categories for a custom post type. However, the built-in WordPress function in_category() does not work with custom taxonomies. So, to figure out if a custom post is in a custom taxonomy, I added this function to my functions.php:

    [php]// Query custom taxonomy category

    function in_custom_category( $category, $_post = null ) {
    if ( empty( $category ) )
    return false;
    if ( $_post ) {
    $_post = get_post( $_post );
    } else {
    $_post =& $GLOBALS[‘post’];
    }

    if ( !$_post )
    return false;

    $r = is_object_in_term( $_post->ID, ‘CUSTOMCAT_SLUG’, $category );
    if ( is_wp_error( $r ) )
    return false;
    return $r;

    }[/php]

    Replace the CUSTOMCAT_SLUG with the slug of your custom taxonomy (for example, movies-cat).

    This returns a true if the post is inside the specified category. You can call this in an if statement such as,

    [php]if (in_custom_category(‘drama’)){

    // it is in the category – do something here

    } else {

    // not in the category, do something else

    }[/php]

    This entry was posted on Tuesday, September 28th, 2010 at 1:31 pm and is filed under WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 0 Comments

    Take a look at some of the responses we have had to this article.

  • Leave a Reply

    Let us know what you thought.

  • Name(required):

    Email(required):

    Website:

    Message: