0

I'm rather new at Wordpress and I the thing I want to do is this:

Create a taxonomy (courses) that works as exactly as POST but with the difference: I need some fields to be added for course (like cost of course, age-span, which day of week the course is held etc.). I guess I could just add custom fields to the POST-taxonomy, but I want to separate "normal posts" with "courses".

I have a lot of experience in PHP-programming but Wordpress is new for me. I understand the concept of taxonomies and hooks and have an understanding how WP works with themes, child themes, plugins etc.

I've installed a plugin called Simple Taxonomy at https://github.com/herewithme/simple-taxonomy and I created a taxonomy called Courses. The thing is that I want to associate a course with a category. When I choose hierachical to true for that specific plugin, then it's only hierachrical within it's own taxonomy but I want the course to have same relation-type as the post-taxonomy.

I've read this How to link categories to custom taxonomy terms? which is similar but I want want to able to select one or more categories to this categories (exactly as you could chose categories for a post-taxonomy)

I bascially want to copy the post-taxonomy and use it as another taxonomy with some other fields.

Is it possible? Or would I have to add fields to the post-taxonomy?

I just understand that there is something called Custom post types http://codex.wordpress.org/Post_Types. Is that the way to go for this issue?

1 Answer 1

1

Yes, you don't need a custom taxonomy, but a custom post type for courses, and probably a custom taxonomy for courses categories.

In WordPress post types are containers for a certain type of information: every information type is a post type.

E.g. Posts are a type and Pages are another type. Every post type (container) contain some informations: an author, a title, a date, a summary (excerpt)... If you need other custom informations (like age-span) you can use custom post fields.

After that, you can group this containers using taxonomies. WordPress has 2 taxonomies (group) that are used to group the standard post type: categories and tags.

When you create your own post type, you can also create your custom taxonomies as well and use them to group your post. Is possible group the CPTs (Custom Post Types) using the core taxonomies, but in most cases is better define custom taxonomies for custom post types.

To create a CPT or custom taxonomies you don't need plugins: in the functions.php file of the theme or in a plugin you can use register_post_type and register_taxonomy to create a CPT and a taxonomy respectively.

Both CPT and custom taxonomy has an option hierachical.

When setted to true for CPT it creates a post type like standard pages, where a post can have a parent, on the countrary, when setted to false it creates a post like the standard post type, where posts can't have parent. The UI created for hierachical and non-hierachical post type is very similar.

A hierachical custom taxonomy is very similar to categories, a non-hierachical one will be similar to post tag. The UI generated by WordPress for hierachical and non hierachical taxonomies is different: in the edit post screen you can choose hierachical taxonomy terms using checkboxes, the non hierachical are chosen using a text field with auto suggestion feature.

Regarding your case a typical code example can be:

function courses_with_cats() {
  $course_labels = array(
    'name' => 'Courses',
    'singular_name' => 'Course',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Course',
    'edit_item' => 'Edit Course',
    'new_item' => 'New Course',
    'all_items' => 'All Courses',
    'view_item' => 'View Course',
    'search_items' => 'Search Courses',
    'not_found' => 'No Course found',
    'not_found_in_trash' => 'No Courses found in Trash',
    'parent_item_colon'  => '',
    'menu_name' => 'Courses'
  );
  $course_args = array(
    'labels' => $course_labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'course' ),
    'capability_type' => 'post',
    'has_archive' => 'courses',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','author','thumbnail','excerpt', 'comments')
  );

  $course_cat_labels = array(
    'name' => 'Courses Category',
    'singular_name' => 'Course Category',
    'search_items' => 'Search Category',
    'all_items' => 'All Categories',
    'parent_item' => 'Parent Category',
    'parent_item_colon' => 'Parent Category:',
    'edit_item' => 'Edit Category',
    'update_item' => 'Update Category',
    'add_new_item' => 'Add New Category',
    'new_item_name' => 'New Category Name',
    'menu_name' => 'Courses Category',
  );
  $course_cat_args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
     'rewrite' => array('slug' => 'courses/category'),
  );

  register_post_type( 'course', $course_args );

  register_taxonomy( 'courses-cat', array('course'), $course_cat_args );
}

add_action( 'init', 'courses_with_cats' );

This code is all you need to register a post type with its own custom taxonomy. This code register a non hierachical post type (like standard posts) with a hierachical custom taxonomy (like standard categories).

Using this standard setup, the url for the archives of courses is something like: http://example.com/courses/. Using http://example.com/courses/category/a-category is shown an archive of all coursesthat belongs to the category with slug "a-category" (probably category name is "A Category").

Look in Codex for the arguments you can use for both register_post_type and register_taxonomy.

For register_post_type note that supports argument change what is shown in post edit screen.

1
  • 1
    Omg. Thanks a lot! I didn't except such a great answer, but I'm very glad for for recieving it! :-) Thanks! Commented Oct 2, 2013 at 6:35

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.