Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have three custom post types created already and their landing pages (page-events.php, etc):

Events, Retreats, Coaching

But for the first, I need some sort of categories which I am going to list on the landing page (page-events.php) as a menu, and inside them I would need blog posts.

I have created a custom taxonomy for this Events custom post type:

register_taxonomy("event", array("event"), array("hierarchical" => true, "label" => "Event Type", "singular_label" => "Event Types ", "rewrite" => true));

The custom post type function for Events:

add_action( 'init', 'create_events' );
function create_events() {
  $labels = array(
    'name' => _x('Events', 'post type general name'),
    'singular_name' => _x('Event', 'post type singular name'),
    'add new, edit, etc, save space'  
    'not_found_in_trash' => __('No Events found in Trash'),
    'parent_item_colon' => ''
  );

  $supports = array('title', 'editor', 'revisions', 'excerpt', 'thumbnail', 'page-attributes');

  register_post_type( 'event',
    array(
      'labels' => $labels,
      'public' => true,
      'has_archive' => true,
      'hierarchical' => true,
      'query_var' => true,
      'supports' => $supports
    )
  );
}

Haven't tried adding posts/pages, have just created them, I can add new Event and chose the Event type in a right side box though. But..

Is this the right approach? Will this work for me, is there anything else I can do better so I won't find a hard time to query for posts belonging to a specific taxonomy under this custom post type?

I've never worked with custom post types and taxonomies and is a bit confusing for me, I just want to make sure I get the right approach.

Thanks.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

No, that is the correct way to do it, but with one suggestion - don't give the custom post type and custom taxonomy the same slug (unless you specify a rewrite name, rather then just 'true') as it will cause problems with your rewrite rules.

If you have problems with the custom types after adding them then swithc your permlinks to default, save them, and then back to what you had them as before (Assuming you even use them).

Querying the posts is easy (once you get your head around it). For example, querying for posts of type 'event' -

$taxonomy = get_query_var('taxonomy'); // If you are going to this page with a taxonomy selected, or you can explicitly declare a taxonomy if you wish
$term = get_query_var('term'); // If you are going to this page with a term selected, or you can explicitly declare a term if you wish
$args = Array( // Array of arguments for query_posts()
    'numberposts' => -1,
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $paged,
    'post_type' => array('events'),
    $taxonomy => $term
);
query_posts($args);
share|improve this answer
    
Thank you @David Gard. But how would I display the titles of the taxonomy pages on my sidebar and their corresponding pages edited in the admin? I have page-xxx for all the custom post types I've created. The above code doesn't work for me. –  C D Feb 27 '12 at 10:42
    
Ok, so correct me if I'm wrong - You have a page that shows posts from your 'Events' Custom Post Type, on which you wish to list posts by the Custom Taxonomy 'Event'? If so, I'm not sure if one query can do that (although you may be able to 'group_by'), but even if not there is a custon query or multiple WP_Query's that we can run. If you confirm my understanding is correct, I will help with the code. –  David Gard Feb 28 '12 at 11:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.