Display custom tags for custom post listing page

by Leanne Seawright   Last Updated November 09, 2018 01:08 AM

I'm very new to WordPress, but I've created a custom post type with two sets of tags. I've added the tags by using the following code:

/**
 * custom tags
 */
function gccsi_create_client_tax() {

    /* Teams */
    register_taxonomy( 
        'post_team', ## tag name
        array( 'team', 'board' ), ## post type
        array( 
            'hierarchical'  => false, 
            'label'         => __( 'Teams', CURRENT_THEME ), 
            'singular_name' => __( 'Team', CURRENT_THEME ), 
            'rewrite'       => true, 
            'query_var'     => true 
        )
    );

    /* Locations */
    register_taxonomy( 
        'post_location', ## tag name
        array( 'team', 'board' ), ## post type
        array( 
            'hierarchical'  => false, 
            'label'         => __( 'Locations', CURRENT_THEME ), 
            'singular_name' => __( 'Location', CURRENT_THEME ), 
            'rewrite'       => true, 
            'query_var'     => true 
        )
    );

}
add_action( 'admin_init', 'gccsi_create_client_tax' );

Which seems to work fine, I'm trying to now display those tags on the main page (which displays all items) so people can filter the listing page. I've tried the following:

$tagsTeam   = get_terms( 'post_team' ); ## I also tried get_terms( 'team' );
echo '<pre>';print_r($tags);echo '</pre>';

Which gives me this:

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

        )

    [error_data] => Array
        (
        )

)

I've also tried using:

the_tags();

But that doesn't display anything. I've searched but I can't seem to find anything else that doesn't need the ID?

Any help would be appreciated!



Answers 1


Taxonomies (and post types) need to be registered on init. You're registering them on admin_init:

add_action( 'admin_init', 'gccsi_create_client_tax' );

This means the taxonomy isn't registered on the front-end. Change the hook to init:

add_action( 'init', 'gccsi_create_client_tax' );
Jacob Peattie
Jacob Peattie
November 09, 2018 00:34 AM

Related Questions





Can't get taxonomy terms by custom field value

Updated November 13, 2017 08:08 AM

Show custom taxonomies of post type, table based

Updated December 14, 2017 23:08 PM