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. Pingback:Customize Custom Post Type Landing Page with Clean URL - Jeffri Hong

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.