Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post type difficulties WordPress


I have a custom post style called "Regions" - with 3 posts in it - title North, South and West. I also have a custom post type called "Parks" - what I want to be able to do is when a user clicks on say the "North" region post it will bring up a bit of text about the region from the body text of that post but then also list all the "Parks" that are specific to that region.

I created some categories inside the Parks custom post type of "North, South and West" and put each park post in its relevant category.

I set up a single-regions.php file to style the regions post page and included a query post function to show the Parks posts, i also managed to filter these by category. But, I can only seem to create one template for all regions. What I want to do is have it pull in a different query depending on which region post it is in. So for the North Region post I can query just the parks in the north category etc.

Am I going about this all wrong I wonder? Is there a better way to use custom posts to achieve what I want. I wonder if maybe I don't need the custom post type Regions. Is there maybe a way to create a template for each Park Category and call that "North Region" . Can you create custom category templates for custom post-types?

Can anyone recommend the best way to achieve what I am looking for. I will also need to go deeper again, once the user clicks on a Park post I want it to bring up park info from the specific park post and then also list all the lodges relevant to that park. Again, I thought I would create a custom post type of "Lodges" and create categories for each of the parks to put them into. But how do I get each park page to show just the lodges in a specific category.

Many thanks!

Answers (1)


Paul Lumsdaine answers:

I think you almost have it. What you will need to create is custom taxonomies. So you would just have the custom post type of "Parks" and have a custom taxonomy of "Regions".

There is a [[LINK href=""]]great tutorial[[/LINK]] that I used to learn all about custom post types. Luckily this author has also written one about custom taxonomies. If you can [[LINK href=""]]follow it through[[/LINK]] you will know exactly how to set this up.

Basically in the tutorial example you will want to replace "cameras" with "parks" and "brands" with "regions".

I have modified the code in the tutorial for you, but I highly suggest reading through the tutorial to gain a greater understanding of whats going on here. You'll be able to place this code in your functions.php

$labels = array(
'name' => 'Regions',
'singular_name' => 'Region',
'search_items' => 'Search Regions',
'popular_items' => 'Popular Regions',
'all_items' => 'All Regions',
'parent_item' => 'Parent Region',
'edit_item' => 'Edit Region',
'update_item' => 'Update Region',
'add_new_item' => 'Add New Region',
'new_item_name' => 'New Region',
'separate_items_with_commas' => 'Separate Regions with commas',
'add_or_remove_items' => 'Add or remove Regions',
'choose_from_most_used' => 'Choose from most used Regions'

$args = array(
'label' => 'Regions',
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'parks/regions', 'with_front' => false ),
'query_var' => true

register_taxonomy( 'regions', 'parks', $args );

register_post_type( 'parks',
'labels' => array(
'name' => __( 'Parks' ),
'singular_name' => __( 'Park' )
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'rewrite' => array( 'slug' => 'parks', 'with_front' => false ),
'has_archive' => true

Let me know if you have any questions!

Jennie Routley comments:

Thank you so much Paul, I have worked through that this morning and now have got my parks showing in the taxonomy-regions.php template.

I wonder if I can trouble you with another question - now I have got this far, do you know how best I could include my "Lodges" I need to be able to make custom posts for each individual lodge and link that up to each Park. However, I am confused as I now have my parks linked up to my regions.

When the user clicks on a park I would like it to be able to show them a list of all the lodges in that park. My brain is getting in nots. Do you know if this would be possible?

I am thinking along the lines of creating a custom post type called "Lodges" then custom taxonomy of Parks. But then how would I make it so my Park post links up to the lodges archive and not the single-parks.php?

Jennie Routley comments:

I managed to work it out with the use of the "Custom Post Template" plugin and a combination of custom taxonomies. Thanks for pointing me in the right direction initially!