1

What do i need to do to enable categories and tags for a custom post type i´ve made? I use the following code:

/* Create custom post type: "Tilbud" */
register_post_type('tilbud', array(
'label' => __('Tilbud'),
'singular_label' => __('Tilbud'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array('title')
));
0

4 Answers 4

11

change your code to this:

/* Create custom post type: "Tilbud" */
register_post_type('tilbud', array(
'label' => __('Tilbud'),
'singular_label' => __('Tilbud'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'taxonomies' => array('post_tag','category'),
'supports' => array('title')
));

the function register_post_type takes a name of the post type and an array of arguments one of these is "taxonomies" and that is what you are missing.

3
  • The categories and tags still don´t show up. Is there anyting else i need to do?
    – user1635
    Jan 18, 2011 at 12:10
  • even with the taxonomies attribute??
    – Bainternet
    Jan 18, 2011 at 12:59
  • yes, even using the code you suggested.
    – user1635
    Jan 18, 2011 at 15:28
3

It worked when I added the following code:

function add_tags_categories() {
register_taxonomy_for_object_type('category', 'tilbud');
register_taxonomy_for_object_type('post_tag', 'tilbud');
}
add_action('init', 'add_tags_categories');
1
  • Nothing else worked except this solution. Thanks A lot!!1 Feb 6, 2016 at 11:59
0

It worked for me using this code:

'taxonomies' => array('post_tag','category'),

Which is added to the same code you're using.

0

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;
}
1
  • that's not what the question is asking
    – Tom J Nowell
    Sep 15, 2023 at 12:05