Custom Post Type/Taxonomy Permalinks


#1

Hey people,

I’ve searched through the ThemeForest forums, WP forums and another 10+ blogs, but still can’t find a solution to this.

Here’s my situation.

I’m trying to create a real estate WordPress theme.

I have created a custom post type called apartments (code used) and added a hierarchical taxonomy (like categories), called locations (code used).

I’ve set permalinks to %category%/%postname%.php

I have added some categories to Locations, like so:

  • New York
    • Brooklyn
  • Los Angeles
  • Cape Town

Now, in a ‘normal’ situation, where you would publish a POST (the built in ones), if I assign it the ‘Brooklyn’ category, I would get the following URL:

http://www.mysite.com/new-york/brooklyn/post-name

However, when using custom post types for the exact same situation, I cannot find a solution to include the Location taxonomy in the URL.

All I get is:
http://www.mysite.com/apartments/post-name

Is there any way I can have the URL like so:

http://www.mysite.com/apartments/brooklyn/post-name.php
or even
http://www.mysite.com/apartments/brooklyn/post-name/

This is driving me NUTS and even after reading through many renowned WP blogs, I can’t figure it out - nothing works…

Any tips are highly appreciated :slight_smile:


#2

I’m not quite sure what you’re trying to do here, but if you’re trying to rename the URL’s you could try URL rewrite method.

http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

Hope this helps!


#3

Hi Di,

I went through the same problem and still are but closer to the solution, in fact what need to be done is to give a slug name to the rewrite argument and to say it is not built in:

$args = array(
     	'labels' => $labels,
       	'public' => true,
       	'show_ui' => true,
	'_builtin' => false, // It's a custom post type, not built in!
       	'capability_type' => 'post',
       	'hierarchical' => false,
       	'rewrite' => array("slug" => "?apartments=","with_front'=>false"), // Permalinks format
       	'query_var' => true,
       	'show_in_nav_menus'=> false,
       	'menu_position' => 5,
       	'supports' => array('title','editor', 'excerpt', 'custom-fields', 'discussion', 'comments', 'author')
        );

so the piece you have to add is:

     '_builtin' => false,
     'rewrite' => array("slug" => "apartments","with_front'=>false"), 

the only problem I have is that doesn’t work without “?” berore and “=” after the slug name:

     'rewrite' => array("slug" => "?apartments=","with_front'=>false"), 

It may help you to find the right way.

If anybody else got the right way, let us know.


#4

Kontent - thanks for the link, but overriding rewrite rules wouldn’t help, since I want to use WordPress

zemotion:

so the piece you have to add is:
     '_builtin' => false,
     'rewrite' => array("slug" => "apartments","with_front'=>false"), 

Thanks buddy, I actually tried that myself with no luck, forgot to specify…

Also, adding ?apartments= to the slug returns some nasty errors in the dashboard:

Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 1 in /home/foldername/public_html/wpinstallation/wp-includes/classes.php on line 210 //(and line 211)

However, I can publish a post, but the url looks like this:

http://www.mysite.com/?apartments/post-name

So still no luck :frowning:


#5
Kontent - thanks for the link, but overriding rewrite rules wouldn't help, since I want to use WordPress

You can use solid url rewrite modules for WordPress then add your own custom method, some links:

http://www.binaryfortress.com/wordpress-url-rewrite/

http://simeonpilgrim.com/blog/2007/11/26/wordpress-url-rewriting/


#6

Also, adding ?apartments= to the slug returns some nasty errors in the dashboard:

Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 1 in /home/foldername/public_html/wpinstallation/wp-includes/classes.php on line 210 //(and line 211)

However, I can publish a post, but the url looks like this:

http://www.mysite.com/?apartments/post-name

So still no luck :frowning:

Did that and it works for me… it also got the same error but I had to flash the permalink, put them back to default save and put back my custom permalink settings


#7

Well, i found a patch solution for that, since i guess it´s a bug on wp 3.0.

On wp-includes/canonical.php line 183 you will find this:

$category = get_term_by('slug', get_query_var('category_name'), 'category');

Patch it replacing with:

$category = get_term_by('slug', end( explode( '/', get_query_var('category_name') ) ), 'category') ;
The problem is that get_term_by doesn't support hierarchies so when passed a second level category e.g. cars/sports it will fail and hence the rewrite is performed loosing the page information.

I had similar problem recently and that´s why i have this on favs, take a look here:

http://core.trac.wordpress.org/ticket/13471

specialy here:

http://core.trac.wordpress.org/ticket/13471#comment:4

I´m gonna check if this has been updated for wp 3.0.1

so, you can apply that patch and see what happens, i haven´t time yet to do it, but please, post here your results if that solved the permalink structure.


#8

Also if this is the solution, could be a good idea to make a hook to include on functions.php and not to hardcode the wp files. :slight_smile:


#9

Hey guys,

Thanks for taking the time to reply, much appreciated :slight_smile:

Unfortunately, it’s a nasty bug that I’ve yet to see a solution for.

@PerfectPixels - did what you said, no luck, it’s stilll showing site.com/?apartments=/post-name

@rg - thanks man, glad to see someone’s on it… Unfortunately, it doesn’t do anything.

Looking at the code in canonical.php, I figured my permalink structure would need to be %category% only, nothing else. This is what I mean:

Line 182 in canonical.php


elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false )

I’ve used different structures, but it doesn’t change anything :frowning:

Guess I’ll have to use custom fields for locations and forget about nice URLs like site.com/apartments/new-york/brooklyn/postname.php :frowning:

Anyway, thanks again for trying to help :slight_smile:


#10

UPDATE!

Ok guys, so I eventually figured it out…somehow…

I’m still registering a custom post type named Apartments, but I am not registering a custom taxonomy!

Instead, I use the default “category” taxonomy on this custom post type, taxonomy which we know is built into the WP core.


'taxonomies' => array('category'),

I then installed the Custom Post Permalinks plugin and, for the “apartments” post type, I specify the following structure:

/apartments/%category%/%apartments%.php

Now, if I have New York as a category, with a sub-category named “Brooklyn”, my permalink looks like this:

http://mysite.com/apartments/new-york/brooklyn/postname.php

Not keen on using the default Categories taxonomy, as the site won’t be in English and I have less flexibility, but for the moment I’ll just have to say thanks and do some hacks for translating the labels in the WP admin (most likely with the .mo file…)

IMPORTANT: it doesn’t work without that plugin :expressionless:

EDIT: since I’ll be using multiple custom post types, I’ll update you if it all works out nice for each of them. Yeah, finally happy :slight_smile:


#11

Just an update to confirm it all works out nice, even when registering multiple custom post types.

The only downside I see is that the taxonomy used will be the same for all content types.

So if you’re using the built in Category taxonomy like I do, you’ll see the same categories everywhere, no matter if you’re writing a regular Post or a custom post type.

Thanks again for the help guys :slight_smile:


#12
Just an update to confirm it all works out nice, even when registering multiple custom post types.

The only downside I see is that the taxonomy used will be the same for all content types.

So if you’re using the built in Category taxonomy like I do, you’ll see the same categories everywhere, no matter if you’re writing a regular Post or a custom post type.

Thanks again for the help guys :slight_smile:

Hi Di,

Thanks for your feedback and glad all works for you. I think I’ll stick with my ?portfolio= permalink until the WordPress 3.1 release which would fix it…

See ya


#13

For things like that i decided not to use post_types in my last theme and i use categories for same thing, also seraching on the net i found this today and i think it´s a very good information:

So, i think i did on the right way. That was the first thing i thought once i discover how post_types works.

Keep using posts, cats and tags is the best way.

As the article says, is


#14

Hey @Di, nice solution, thanks. Just one question, I’m trying the exact structure, but I get 404 on my single-post_type.php templates.

If you can put here how do you manage your register_post_type setup, will be great too.

thanks


#15
Hey @Di, nice solution, thanks. Just one question, I'm trying the exact structure, but I get 404 on my single-post_type.php templates.

If you can put here how do you manage your register_post_type setup, will be great too.

thanks

See the “code used” links at the beginning of the thread and make sure you replace this:

'taxonomies' => 'locations',

with this:

'taxonomies' => 'category',

and forget about adding a custom taxonomy…


#16

ooops, my question: how do you manage your templates using that Custom Post Permalinks plugin?


#17

ooops, my question: how do you manage your templates using that Custom Post Permalinks plugin?


#18
ooops,, my question: how do you manage your templates using that Custom Post Permalinks plugin?

Ah… well you must go to Settings > Permalinks and you can see there a new field added. (it adds one for every custom post type you create)

I opted for:

/apartments/%category%/%apartments%.php

#19

Hey @Di thanks for fast reply… I totally miss that link… thanks. I try your solution. When I put this:

‘taxonomies’ => ‘category’,

on my arguments for register_post_type, I got this error:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\mywebsite\wp-includes\post.php on line 886

I have this on the extra permalink setup:

/%category%/%post_type%/%concerto%/

I have one post type, called “concerto”. But will be others, using the same category structure. My webiste must work like this:

www.website.com/category/post_type/post_name/

I trying to achieve that “category” before the post type…

Before your code, I was using this code to register category as taxonomies for all posts types, including the default Wordpress “post”:

register_taxonomy('category',array('post','concerto'), array( 'hierarchical' => true, 'show_ui' => true, 'query_var' => true, //'rewrite' => array( 'slug' => 'category' ), 'rewrite' => true, ));

The code: //‘rewrite’ => array( ‘slug’ => ‘category’ ), is commented.
Any ideas?

Thanks,

Homem Robô


#20

Hey @Di, even with

‘taxonomies’ => array(‘category’),

my templates get 404…

I’ll keep trying,

thanks,