I want to get all categories (custom taxonomy) in array from a specific post type. The taxonomy (category) registered for a specific post type only. And I want to retrieve all categories in an array. Be like -

  'Category Name 1' => 'slug-name-1',
  'Category Name 2' => 'slug-name-2',
  'Category Name 3' => 'slug-name-3',

It can be get category names by slug or id.

  • To build up that array for a given custom taxonomy can be easily achieved, but I find ambiguous what you intend at all. When you say "as list in array from a specific post type" do you mean the custom taxonomy is shared among different post types? And what do you mean with "It can be get category names by slug or id"?
    – Luis Sanz
    Apr 3, 2016 at 21:04
  • 2
    Use get_terms , then loop through the array of term objects and create your array Apr 4, 2016 at 5:45
  • @LuisSanz I've updated the question. The taxonomy (category) registered for a specific post type only. And I want to retrieve all categories in an array. Apr 4, 2016 at 5:51
  • I just wonder if you aren't looking for something like this Apr 4, 2016 at 5:58
  • 1
    Okay, I'll take a look at this. Thanks for the reference. Apr 4, 2016 at 6:27

2 Answers 2


You can use get_terms() to retrieve the custom taxonomy terms and then build up a custom array to match the structure you want.

Try this:

//Get the custom taxonomy terms
$taxonomies = array(
    'name' => 'your_custom_taxonomy' //Edit to match your needs

$taxonomy_terms = get_terms( $taxonomies );

//This array will store the results
$taxonomy_array = array();

//Parse the terms
foreach ( $taxonomy_terms as $taxonomy_term ) :

    //Get the taxonomy term name
    $taxonomy_term_name = $taxonomy_term->name;

    //Get the taxonomy term slug
    $taxonomy_term_slug = $taxonomy_term->slug;

    //Push the custom array
    $taxonomy_array[ $taxonomy_term_name ] = $taxonomy_term_slug;


The data will be stored in $taxonomy_array following the format you provided. You can pass additional arguments to get_terms(), such as the order.


If you want to retrieve all of the terms that are within all of the registered taxonomies for a specific post type you can use a function like the one below.

function termsByTaxonomiesPostType( $postType ) {

    if ( ! post_type_exists( $postType ) ) {
        return [];

    $list       = [];
    $taxonomies = get_object_taxonomies( $postType );

    if ( empty( $taxonomies ) ) {
        return [];

    foreach ( $taxonomies as $taxonomy ) {
        $terms = get_terms( [
            'taxonomy' => $taxonomy,
            'fields'   => 'all',
        ] );

        // Not an array if you pass 'count' as value for 'fields'.
        if ( ! is_array( $terms ) || is_wp_error( $terms ) ) {

        $list[ $taxonomy ] = [];

        // Note: if you want 'id=>slug', 'id=>name', 'names', 'id=>parent', 'ids', 'slugs' only
        // You dont need to perform this.
        foreach ( $terms as $term ) {
            $list[ $taxonomy ] = array_merge(
                $list[ $taxonomy ],
                    $term->name => $term->slug,

    return $list;

You're list will contain a list of taxonomies and for each taxonomy a list of key => value pairs where the key is the name of the term and the value the slug.

To note that if you want to retrieve 'id=>slug', 'id=>name', 'names', 'id=>parent', 'ids', 'slugs' only you don't need to perform an additional loop because WordPress will do it for you.

To retrieve one of the associations listed above, just set the string as value for the key fields.

  • You may also cache the list based on the post type name if you think to use the same function multiple time during the same http request.
    – guido
    Jun 19, 2018 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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