Search code examples
wordpresscustom-post-type

Display list of tags associated with custom post type


I am having a hard time to produce a list of tags used for a custom post type.

* Post type: *

resource

* Tags associated with resource: *

General Information
Communications Committee
Keynote Documents
Policy Postions

* Desired Output: *

<ul>
<li>General Information</li>
<li>Communications Committee</li>
<li>Keynote Documents</li>
<li>Policy Positions</li>
</ul>

* Attempt so far: *

$terms = get_terms( array(
        'taxonomy' => 'resources',
        'hide_empty' => false,
    ) );

print_r($terms);

Results in:

WP_Error Object ( [errors] => Array ( [invalid_taxonomy] => Array (
 [0] => Invalid taxonomy. ) ) [error_data] => Array ( ) )

What am I doing wrong here? Why isn't it outputting what I think it should, and how can I get there?

Thanks


Solution

  • You are passing the custom post type registered name to the get_terms which is why it's not working.

    Based on your comments, you need to pass resource_type which is the registered taxonomy name.

    This is how your term query should look:

    $terms = get_terms(array(
        'taxonomy' => 'resource_type',
        'hide_empty' => false,
    ));
    

    To output the results as you wish (without links):

    $terms = get_terms(array(
            'taxonomy' => 'resource_type',
            'hide_empty' => false,
        ));
    
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
         echo '<ul>';
         foreach ( $terms as $term ) {
             echo '<li>' . $term->name . '</li>';
         }
         echo '</ul>';
    }