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?
get_categories()
does not acceptpost_type
as an argument, usetaxonomy
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.Instead of type you have to set post_type , by default get_categories try to hide empty categories , if you want display it all add hide_empty property set to false
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:
While quick reading @ioni’s answer; it looks like it would give you a proper result it will most likely be pretty slow as it goes through every post and then looks for all terms that belong to each post individually. This will easily result in thousands of SQL queries.
My recommendation is to use a single query to get the IDs of all terms that belong to a post type. Then use those ids to filter
get_terms()
.MySQL query:
PHP function:
Usage: