2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

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 :)

193 posts
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 100 and 1 000 dollars
Kontent says

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!

35 posts
  • Author had a Free File of the Month
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 100 and 1 000 dollars
  • United Kingdom
zemotion says

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.

2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

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 :(

193 posts
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 100 and 1 000 dollars
Kontent says
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/

64 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • France
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
PerfectPixels says

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 :(

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

688 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 5 000 and 10 000 dollars
  • Uruguay
_rg_ says

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.

688 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 5 000 and 10 000 dollars
  • Uruguay
_rg_ says

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. :)

2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

Hey guys,

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

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 :(

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

Anyway, thanks again for trying to help :)

2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

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 :|

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 :)

by
by
by
by
by
by