close
categoriesforcpts

Users want to know that is it possible to add categories to a they have created. Categories are one of the built-in taxonomies in . By default they appear only for posts. But in some scenarios you may want to share them with a custom post type as well. This article will explain display multiple post types on your category archive page.

The Plugin Method

Those who are not that experienced, it is recommended to use Custom Post Type UI plugin to create custom post types. When using Custom Post Type UI plugin, you have the option to associate your custom post type to any built-in or custom taxonomy including categories.

First you need to install and activate the Custom Post Type UI plugin, after it is activated you need to visit CPT UI » Add/Edit Post Types to create a new custom post type or edit an existing custom post type you created with the plugin.

editposttypes

Scroll down on the Advanced Options to the bottom and there you will see the Built in Taxonomies option. Check the box next to categories and save your custom post type.

enablecategoriesforcpt

Click on save post type button to store your changes.

Manually Adding Categories to a Custom Post Type

If you created your custom post type by adding the code in your theme’s functions.php file or a site-specific plugin, then you will have to modify the code to add category as supported taxonomy.

All you need to do is add this line in the arguments for your CPT.

1’taxonomies’  => array( ‘category’ ),

 

It is likely that you may already have this line in the existing code for your CPT with some other custom taxonomy in it. If you do, then you just need to add a comma after that and add category, like this:

1’taxonomies’          => array(‘topics’, ‘category’ ),

 

Here is a full example code where we have created a custom post type called movies with support for built-in categories.

01function custom_post_type() {

 

02 

 

03// Set UI labels for Custom Post Type

 

04    $labels = array(

 

05        ‘name’                => _x( ‘Movies’, ‘Post Type General Name’, ‘twentythirteen’ ),

 

06        ‘singular_name’       => _x( ‘Movie’, ‘Post Type Singular Name’, ‘twentythirteen’ ),

 

07        ‘menu_name’           => __( ‘Movies’, ‘twentythirteen’),

 

08        ‘parent_item_colon’   => __( ‘Parent Movie’,’twentythirteen’ ),

 

09        ‘all_items’           => __( ‘All Movies’,’twentythirteen’ ),

 

10        ‘view_item’           => __( ‘View Movie’,’twentythirteen’ ),

 

11        ‘add_new_item’        => __( ‘Add New Movie’,’twentythirteen’ ),

 

12        ‘add_new’             => __( ‘Add New’,’twentythirteen’ ),

 

13        ‘edit_item’           => __( ‘Edit Movie’,’twentythirteen’ ),

 

14        ‘update_item’         => __( ‘ Movie’,’twentythirteen’ ),

 

15        ‘search_items’        => __( ‘Search Movie’,’twentythirteen’ ),

 

16        ‘not_found’           => __( ‘Not Found’,’twentythirteen’ ),

 

17        ‘not_found_in_trash’  => __( ‘Not found in Trash’,’twentythirteen’ ),

 

18    );

 

19 

 

20// Set other options for Custom Post Type

 

21 

 

22    $args = array(

 

23        ‘label’               => __( ‘movies’, ‘twentythirteen’),

 

24        ‘description’         => __( ‘Movie news and reviews’,’twentythirteen’ ),

 

25        ‘labels’              => $labels,

 

26        ‘supports’            => array( ‘title’, ‘editor’,’excerpt’, ‘author’, ‘thumbnail’, ‘comments’, ‘revisions’,’custom-fields’, ),

 

27        ‘hierarchical’        => false,

 

28        ‘public’              => true,

 

29        ‘show_ui’             => true,

 

30        ‘show_in_menu’        => true,

 

31        ‘show_in_nav_menus’   => true,

 

32        ‘show_in_admin_bar’   => true,

 

33        ‘menu_position’       => 5,

 

34        ‘can_export’          => true,

 

35        ‘has_archive’         => true,

 

36        ‘exclude_from_search’ => false,

 

37        ‘publicly_queryable’  => true,

 

38        ‘capability_type’     => ‘page’,

 

39 

 

40        // This is where we add taxonomies to our CPT

 

41        ‘taxonomies’          => array( ‘category’ ),

 

42    );

 

43 

 

44    // Registering your Custom Post Type

 

45    register_post_type( ‘movies’, $args );

 

46 

 

47}

 

48 

 

49/* Hook into the ‘init’ action so that the function

 

50* Containing our post type registration is not

 

51* unnecessarily executed.

 

52*/

 

53 

 

54add_action( ‘init’, ‘custom_post_type’, 0 );

 

Displaying Multiple Post Types on Category Page

By default the category pages on your WordPress site will only display the default ‘Posts’ post type. To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php or a site-specific plugin.

01add_filter(‘pre_get_posts’, ‘query_post_type’);

 

02function query_post_type($query) {

 

03  if( is_category() ) {

 

04    $post_type = get_query_var(‘post_type’);

 

05    if($post_type)

 

06        $post_type = $post_type;

 

07    else

 

08        $post_type = array(‘nav_menu_item’, ‘post’, ‘movies’);// don’t forget nav_menu_item to allow menus to work!

 

09    $query->set(‘post_type’,$post_type);

 

10    return $query;

 

11    }

 

12}

 

Don’t forget to replace movies with the name of your own custom post type.

Tags : custom post typewordpressWordPress how to
Nooruddin Ahmed

The author Nooruddin Ahmed

An avid football fan. Lives for the weekend game.Aviation enthusiast. Believes in letting bygones be bygones.

Leave a Response