Take the 2-minute tour ×

I have a Custom Post Type, 'ioni_codex' I am using built-in Wordpress category as taxonomy

I want to list all categories used by 'ioni_codex'.

I assume that this code will do the trick:

$myargs = array (
    'type' => 'ioni_codex'
$categories = get_categories( $myargs );

However instead I see the list of all categories not the categories assigned to by 'ioni_codex'.

What Am I doing wrong?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

get_categories() does not accept post_type as an argument, use taxonomy instead and refer to the taxonomy by the name you gave it when registering it. Here is a link to the codex which can explain it in more detail - http://codex.wordpress.org/Function_Reference/get_categories.

share|improve this answer
I can use 'type', it still does not procue the desired result. I looked into get_terms and it appears that it does not account for post_type whatsoever. What I need is to list categories in which the 'ioni_codex'appears. Instead it lists me all categories –  ioni Jul 27 '12 at 0:48
custom post types don't have categories. –  Grávuj Miklós Henrich Jul 27 '12 at 7:36
I just noticed that you mentioned you are using the default WordPress categories in your custom post type (CPT). To get the categories used only for your CPT you would need to create a separate taxonomy and register it with your CPT. Here is a link to Custon Taxonomy Codex Page : codex.wordpress.org/Taxonomies#Custom_Taxonomies –  WH Web Dev Jul 27 '12 at 13:49
I got half of answer in a sister project: –  ioni Jul 27 '12 at 14:13
add comment

I have an answer at a sister project: Bainternet♦ has re-written get_terms() function to provide for the post_type

Please refer to his solution here or just copy and past from below:

/* get terms limited to post type 
 @ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from. 
 @ $args  -  (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
 @ $post_type - (string|array) of post types to limit the terms to
 @ $fields - (string) What to return (default all) accepts ID,name,all,get_terms. 
 if you want to use get_terms arguments then $fields must be set to 'get_terms'
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
    $args = array(
        'post_type' => (array)$post_type,
        'posts_per_page' => -1
    $the_query = new WP_Query( $args );
    $terms = array();
    while ($the_query->have_posts()){
        $curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
        foreach ($curent_terms as $t){
          //avoid duplicates
            if (!in_array($t,$terms)){
                $terms[] = $c;
    //return array of term objects
    if ($fields == "all")
        return $terms;
    //return array of term ID's
    if ($fields == "ID"){
        foreach ($terms as $t){
            $re[] = $t->term_id;
        return $re;
    //return array of term names
    if ($fields == "name"){
        foreach ($terms as $t){
            $re[] = $t->name;
        return $re;
    // get terms with get_terms arguments
    if ($fields == "get_terms"){
        $terms2 = get_terms( $taxonomies, $args );
        foreach ($terms as $t){
            if (in_array($t,$terms2)){
                $re[] = $t;
        return $re;
share|improve this answer
add comment

Your Answer


