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

Problem with custom post types, taxonomy and permalinks WordPress

  • REFUNDED

I am experiencing problems with custom post types, taxonomies, permalinks and rewrites.

I created a custom "recipes" post type and taxonomy using this code:


add_action('init', 'recipes_register');
function recipes_register() {
$labels = array(
'name' => __('Recipes', 'framework'),
'singular_name' => __('Recipe', 'framework'),
'add_new' => __('Add Recipe', 'framework'),
'add_new_item' => __('Add New Recipe', 'framework'),
'edit_item' => __('Edit Recipe', 'framework'),
'new_item' => __('New Recipe', 'framework'),
'view_item' => __('View Recipe', 'framework'),
'search_items' => __('Search Recipe', 'framework'),
'not_found' => __('Nothing found', 'framework'),
'not_found_in_trash' => __('Nothing found in Trash', 'framework'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/img/admin-recipes.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','comments'),
);
register_post_type( 'recipes' , $args );
}

register_taxonomy('recipe_type', array('recipes'), array('hierarchical' => true, 'label' => __('Recipe Type', 'framework'), 'rewrite' => true)); //array('hierarchical' => true, 'slug'=>'recipes')



Please note that array('hierarchical' => true, 'slug'=>'recipes') is commented out.

The structure of my taxonomies and custom post type is as follows:

recipe_type
- Fish
- Meat
-- Beef
--- Fillet steak royale [Custom post]

In the register_taxonomy() function, if I don't set the rewrite option as shown above (commented out in the code) then the following URLs work, but are not in the format I need:

- mysite.com/recipe_type/
- mysite.com/recipe_type/meat/
- mysite.com/recipe_type/meat/beef/
- mysite.com/recipes/fillet-steak-royale

Additionally, on the single-recipes.php page (mysite.com/recipes/fillet-steak-royale) I don't seem to be able to access the ancestors required in the breadcrumb.

Then, if I set the rewrite option with:

"rewrite" => array("hierarchical" => true, 'slug' => 'recipes')));

After that, these URLs works fine:
- mysite.com/recipes/
- mysite.com/recipes/meat/
- mysite.com/recipes/meat/beef/

But unfortunately these URLs now throw a 404 error:

- mysite.com/recipes/meat/beef/fillet-steak-royale
- mysite.com/recipes/fillet-steak-royale

What do I have change to get the first URL to work so that the custom post type temple (single-recipes.php) get loaded and has meat and beef as ancestors that I can use in the breadcrumb?

I am a bit stuck on this and I would be grateful if you could help me with this.

Thanks

Thomas

Answers (2)

2011-06-24

Linda answers:

Hi, do you have a live site we could view? Would it be possible to get access so I could take a look at the code?


Thomas comments:

Sorry, this is for a client project and we don't want to make it public yet.

Can you send me an email address and I will send you the URL.

2011-06-26

Jurre Hanema answers:

Hi,

Clearly, your problem is caused by the fact that you are trying to use the same slug ("recipes") for both the individual recipes (posts) and the taxonomy. This means that Wordpress cannot distinguish between a query for a single recipe or a query for a term in the recipe_type taxonomy.

For this reason, I strongly doubt if it is at all possible to realize your proposed URL structure. And even if it is, I think it's going to be more trouble than it's worth.

If this were my project, I would advise the following:

- Rename the post type to "recipe" instead of "recipes". (Note that this is also more correct because a post in your case represents exactly one recipe: no more, no less.)
- Keep the slug for recipe_type set to "recipes"

This would then lead to the following, semantically correct, URL structure:

mysite.com/recipes/
mysite.com/recipes/meat/
mysite.com/recipes/meat/beef/
mysite.com/recipe/fillet-steak-royale

This problem reminds me of a site I did for a client - except that it was a portfolio of websites, not recipes. In their case we decided to give the taxonomy the slug "portfolio" and simply call the post type "website".

Now, assuming that you implement my advice above, you should evidently rename the template single-recipes.php to single-recipe.php. You can then simply access the terms you need for the breadcrumb using [[LINK href="http://codex.wordpress.org/Function_Reference/get_the_terms"]]get_the_terms()[[/LINK]].