J.me

Some useful tips for WordPress custom post type and taxonomy

Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have register_post_type and register_taxonomy created specifically for this purpose.

While this two combined to be a wonderful addition for your WordPress blog, there is still a lot of things that is not yet documented. Here is a few useful tips I collected when working with custom post type and taxonomy.

Add taxonomy to more than one post type

There is few ways to doing this, depend to your code. If you register the post type first, then when register taxonomy, you’ll be able to include it to any post types you want.

Example (taken from codex).

1
2
3
4
5
6
7
  register_taxonomy('genre',array('book','magazine','post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'genre' ),
  ));

See the line 1 above, the second parameter for register_taxonomy function is the list of post types you can include. It accept both string and array, as you can expect, we use array when we want to include many post type, and use string when you only need one. The same thing is applied if you register the taxonomy first, you can assign any taxonomy you like when register the post type.

Example (taken from codex)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'taxonomies' => array('genre','grade','category'),
    'supports' => array('title','editor','author','thumbnail','excerpt','comments')
  ); 
  register_post_type('book',$args);

As you can see, the taxonomies parameter can be used to assign any taxonomy you like to the post type you just registered. Again, it accept both array and string.

But what if you want to add taxonomy to certain post type later, after they are defined? WordPress have a function to do this, it is register_taxonomy_for_object_type. Take a look on the example below.

Example

1
register_taxonomy_for_object_type('genre', 'book');

Handy, huh?

Querying more than one post type

Let’s say, for example, you have defined post type for book, comic and magazine. Then, somewhere in your web page, you want to show the latest list of all of these post types together. As you might know already, custom post type is not included in normal WordPress loop, so you’ll need to add your own query and loop, whether by using query_posts, get_posts or by creating a new instance of WP_Query object.

Example

1
2
3
4
5
6
7
8
$args = array(
    'post_type' => array('book', 'comic', 'magazine'),
    'posts_per_page' => 5
);
query_posts($args);
while ( have_posts() ): the_post;
    // and so on
endwhile;

See the line 2. You can add any post type you need in the post_type parameter. As always, it also accept both array and string.

Querying the custom taxonomy

You know how to querying multiple post type now, but how if you want to filter it by taxonomy? While post category and tags offer many ways to filter it, the custom taxonomy is still limited. The only way I can find right now is to filter the taxonomy by the slug.

Example

1
2
3
4
5
6
7
8
9
10
$args = array(
    'post_type' => array('book', 'comic', 'magazine'),
    'taxonomy' => 'genre',
    'term' => array('science', 'fiction'),
    'posts_per_page' => 5
);
query_posts($args);
while ( have_posts() ): the_post;
    // and so on
endwhile;

From the example above, I filtered the result by including only science and fiction genre from all three post types we defined. Unfortunately, I haven’t find any way to filter it by multiple taxonomies at once.

The custom post type feed

Custom post type also have its own feed. Depend to your permalink structure, you can find it by either

http://www.yourwordpresswebsite.com/?feed=rss2&post_type=book

or

http://www.yourwordpresswebsite.com/feed/?post_type=book

Unfortunately, again, I can’t find any way to get the feed for multiple post type.

So that’s it for now. If you find any good tips, you can share it in comment. πŸ™‚

10 comments | Leave a comment

  1. wil July 25, 2010 23:16

    nice tips thx,
    how its possible to add automatically a taxonomy when a post published ?

    • keaglez July 26, 2010 04:04

      I don’t really get your question. As long as you have added the taxonomy to the post type, you can choose any of the term in the taxonomy when editing the post. πŸ™‚

  2. Lionel - Websourcing.fr August 14, 2010 06:25

    “Unfortunately, again, I can’t find any way to get the feed for multiple post type.”

    you can set up the standard feed to get all post types, including ‘post’with a filter in your functions.php file :

    add_filter( ‘pre_get_posts’, ‘my_get_posts’ );
    function my_get_posts( $query ) {
    if (is_feed())
    $query->set( ‘post_type’, array( ‘post’, ‘mycustomtype’, ‘anothercustomtype’ ) );
    return $query;

    Hope it will help…
    }

    • keaglez August 14, 2010 06:29

      Awesome!

      Thanks for the tips Lionel. πŸ™‚

  3. Alan September 16, 2010 21:17

    I just want to query a taxonomy, is that what this is suppose to do?

    I’ve got a post type “Bands” with a taxonomy “Genres” and say one genre of “Rock.” I want to create a page that lists all of the bands listed under “Rock.”

    I think I’ve tried every tutorial on the internet that claims to know how to do this but none of them work.

    • keaglez September 22, 2010 12:56

      Just create a page template for the “Rock”. Then use the query for custom taxonomy above. For example:

      $args = array(
      ‘post_type’ => ‘band’,
      ‘taxonomy’ => ‘genre’,
      ‘term’ => array(‘rock’),
      ‘posts_per_page’ => 5
      );
      query_posts($args);

      And then loop the result by the usual WordPress Loop.

  4. teguh November 3, 2010 07:52

    Pusing gan Jeff bacanya πŸ˜€ Ayo bantuin kasih ane solusi aja gan, di thread wordpress di kaskus. Yg pake nick tukang ojek. Ditunggu pencerahannya gan.

  5. Pingback: Customize Custom Post Type Landing Page with Clean URL - Jeffri Hong

  6. Rendy May 3, 2013 10:41

    Nice Tips… Thx…!!!

  7. Wiladatika January 27, 2015 03:20

    Nice article… thx πŸ™‚

Leave a comment