1

I'm using the cherry framework on a WP site. It comes with a custom post type that can be used to add 'Team Members' and create staff pages etc.

I need to expand this so that I can add tags to each 'team member' inorder that I can essentially tag them as working in a department a / b / c / etc.

The custom post type is registered in the theme-init.php file using this code;

/* Our Team */
function my_post_type_team() {
register_post_type( 'team',
    array(
        'label'               => theme_locals("our_team"),
        'singular_label'      => theme_locals("our_team"),
        '_builtin'            => false,
        // 'exclude_from_search' => true, // Exclude from Search Results
        'capability_type'     => 'page',
        'public'              => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => false,
        'menu_position'       => 5,
        'menu_icon'           => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-businessman' : '',
        'rewrite'             => array(
                                    'slug'       => 'team-view',
                                    'with_front' => FALSE,
                                ),
        'supports' => array(
                        'title',
                        'editor',
                    'thumbnail',
                    )
    )
);
}
add_action('init', 'my_post_type_team');

I want to add tags to this, so that when I add a new team member, I can also assign them to a department by means of adding a given relevant tag. At present the tag editor box doesn't appear on the add new / edit page.

So, I adapted the above code to include a register taxonomy like this;

/* Our Team */
function my_post_type_team() {
register_post_type( 'team',
    array(
        'label'               => theme_locals("our_team"),
        'singular_label'      => theme_locals("our_team"),
        '_builtin'            => false,
        // 'exclude_from_search' => true, // Exclude from Search Results
        'capability_type'     => 'page',
        'public'              => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => false,
        'menu_position'       => 5,
        'menu_icon'           => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-businessman' : '',
        'rewrite'             => array(
                                    'slug'       => 'team-view',
                                    'with_front' => FALSE,
                                ),
        'supports' => array(
                        'title',
                        'editor',
                    'thumbnail',
                    )
    )
);
register_taxonomy(
    'team_tag',
    'team',
    array(
        'hierarchical'  => false,
        'label'         => theme_locals("tags"),
        'singular_name' => theme_locals("tag"),
        'rewrite'       => true,
        'query_var'     => true
    )
);
}
add_action('init', 'my_post_type_team');

However, I'm still not getting the tag box showing up on the edit page in admin.

Any help with this will be greatly appreciated.

2
  • What is the function of these theme_locals Sep 9, 2014 at 12:40
  • @PieterGoosen Ive no idea to be honest. They must be part of the framework (cherry framework). Sep 9, 2014 at 13:35

6 Answers 6

2

It seems the issue was in part down to their being a theme-init.php in the child theme which was overwritting parts of the theme-init.php in the parent / cherry framework theme.

I resolved the issue by adding the following code into my child theme's theme-init.php;

register_taxonomy('team_tag', 'team', array(
    'hierarchical' => false, 
    'label' => theme_locals("tags"), 
    'singular_name' => theme_locals("tag"), 
    'rewrite' => true, 
    'query_var' => true
    )
);
1
register_post_type( 'team',
    array(
        'label'               => theme_locals("our_team"),
        'singular_label'      => theme_locals("our_team"),
        '_builtin'            => false,
        // 'exclude_from_search' => true, // Exclude from Search Results
        'capability_type'     => 'page',
        'public'              => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => false,
        'menu_position'       => 5,
        'menu_icon'           => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-businessman' : '',
        'rewrite'             => array(
                                    'slug'       => 'team-view',
                                    'with_front' => FALSE,
                                ),
        'supports' => array(
                        'title',
                        'editor',
                    'thumbnail',
                    ),
        'taxonomies' => array('team_tag')
    )
);

You can see that I added the taxonomies parameter to the register_post_type function.

Although this code should work, you could try creating the relation later, with this:

add_action('init', 'add_tax_post_rel');

function add_tax_post_rel() {
    register_taxonomy_for_object_type('team_tag', 'team', 11);
}
4
  • Thanks for the answer, but that doesn't do anything. If I change 'team_tag' to 'tag' and then add 'tags' to the support array, I get the tag box in the submenu and on the editor page. however this uses the sitewide tags. Sep 9, 2014 at 12:26
  • @PhillHealey can't be, the build-in tags are under the taxonomy post_tag, not tag Sep 9, 2014 at 12:42
  • @PhillHealey, I added an alternative way to create the relation.
    – Tomás Cot
    Sep 9, 2014 at 12:57
  • @PieterGoosen Ok, it uses the same tags as another custom post type then. Sep 9, 2014 at 13:18
1

try this

register_taxonomy(
        'team_tag', 
        'team', 
        array( 
            'hierarchical'  => false, 
            'label'         => __( 'Tags', CURRENT_THEME ), 
            'singular_name' => __( 'Tag', CURRENT_THEME ), 
            'rewrite'       => true, 
            'query_var'     => true 
        )  
    );
2
  • 1
    Please add proper answers, that is, if you add code, explain what your code does andhow it works. Thanks Dec 1, 2015 at 8:49
  • This does have nearly identical code without an opaque function definition that users don't have access to. In many ways this should be the code used in the accepted answer as it applies to all WP, not just cherry framework if there is no code comment or function definition. It even prompted a question from you.
    – MrMesees
    Jun 13, 2017 at 12:03
1

You can try this

add_action( 'init', 'create_client_tax' );
function create_client_tax() {
    register_taxonomy( 
            'client_tag', //your tags taxonomy
            'client',  // Your post type
            array( 
                'hierarchical'  => false, 
                'label'         => __( 'Tags', CURRENT_THEME ), 
                'singular_name' => __( 'Tag', CURRENT_THEME ), 
                'rewrite'       => true, 
                'query_var'     => true 
            )  
        );
}

May be this will help you

1

I was also missing the tags box on admin CPT edit screen. It appeared with adding 'show_in_rest' => true, in the register_taxonomy() params.

1

If you need to output tags for custom post type - here good example https://jamper.online/vyvod-tegov-tags-dlya-kastomnyh-postov-custom-post-type

    function get_terms_by_custom_post_type( $post_type, $taxonomy ){
    $args = array( 'post_type' => $post_type);
    $loop = new WP_Query( $args );
    $postids = array();
    // build an array of post IDs
    while ( $loop->have_posts() ) : $loop->the_post();
        array_push($postids, get_the_ID());
    endwhile;
    // get taxonomy values based on array of IDs
    $taxonomies = wp_get_object_terms( $postids,  $taxonomy );
    wp_reset_postdata();
    return $taxonomies;
}

Your Answer

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

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