0

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.

1 Answer 1

0

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);
2
  • 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. Commented Feb 27, 2012 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
    Commented Feb 28, 2012 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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