Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-use default categories/tags taxonomies for custom post type?

In my wordpress theme's functions.php, I've created a custom post type called foobar.

Is it possible to re-use the default categories and tags for foobar posts? Or do I have to create two taxonomies:

  • one for categories
  • the other for tags

to achieve this?

EDIT: I think I've solved this by using this code within the function that creates the custom post type:

register_taxonomy_for_object_type('category', 'foobar');
register_taxonomy_for_object_type('post_tag', 'foobar');
like image 843
CaseTA Avatar asked Jun 06 '11 23:06

CaseTA


People also ask

How do I display the taxonomy of a custom post type in WordPress?

You should be able to use get_the_terms() to do this. <? php $loop = new WP_Query( array( 'post_type' => 'projets', 'posts_per_page' => '1' ) ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); // get all of the terms for this post, with the taxonomy of categories-projets.

What is difference between taxonomy and category WordPress?

What is a Taxonomy. In broad terms, any method of grouping of posts is known as a Taxonomy. A Category and Tags are two methods of grouping posts that are present in WordPress by default. hese are the Taxonomies that are present by default in WordPress.

How do I add custom taxonomy to custom post type?

' So make sure you have a custom post type created before you begin creating your taxonomies. Next, go to CPT UI » Add/Edit Taxonomies menu item in the WordPress admin area to create your first taxonomy. On this screen, you will need to do the following: Create your taxonomy slug (this will go in your URL)

Which of the following contains only default taxonomies in WordPress?

The default taxonomies in WordPress are: categories: a hierarchical taxonomy that organizes content in the post Post Type. tags: a non-hierarchical taxonomy that organizes content in the post Post Type. post formats: a method for creating formats for your posts.


1 Answers

You can kill two birds with one stone and use the taxonomies key when registering the post type:

$product_labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Product', 'post type singular name'),
        'add_new' => _x('Add New', 'portfolio item'),
        'add_new_item' => __('Add New Product'),
        'edit_item' => __('Edit Product'),
        'new_item' => __('New Product'),
        'view_item' => __('View Product'),
        'search_items' => __('Search Products'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );
    $product_args = array(
        'labels' => $product_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'product'),
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 2,
        'supports' => array('title','editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'page-attributes', 'custom-fields', ),
        // Set the available taxonomies here
        'taxonomies' => array('category', 'post_tag') 
    );
    register_post_type('product', $product_args);
like image 132
Aram Kocharyan Avatar answered Oct 21 '22 15:10

Aram Kocharyan