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

Including Custom Post Types and Taxonomies in page hierarchy WordPress

  • SOLVED

My current WP site exists only of static pages and sub-pages. Now I´m trying to include Custom Post Types and Taxonomies in this page hierarchy.

This is what I have now:
example.com/<strong>hotels</strong> --> Page
example.com/hotels/<strong>my_hotel</strong> --> Custom Post Type <em>hotel</em>*

(* = I´m manually setting the page as a parent of the CPT, here´s [[LINK href="http://www.gravitationalfx.com/how-to-set-a-page-as-the-parent-of-a-wordpress-custom-post-type/"]]how[[/LINK]])


So far, so good. Now this is where I´m stuck.

The Custom Post Type <em>hotel</em> has several taxonomies attached to it. For example a <strong>rating</strong>-taxonomy with the term <strong>5-stars</strong>. By default, this term links to the archive page: <em>example.com/<strong>rating/5-stars</strong> </em>

Instead of resolving to this default URL, I want the taxonomy term to link to the sub page:
example.com/<strong>hotels/5-stars/</strong>

This sub-page uses a custom page template that shows all hotels using the <em>5 star</em> taxonomy term. The default taxonomy URL <em>example.com/rating/5-stars</em> would not exist

I´m looking for the best (scalable & future proof) way to set this up in WP, ideally using as much of WPs default funtionality as possible. This is what I´ve found untill now:

- Just Tadlock proposed redirecting term archives to pages as a possible solution ([[LINK href="http://justintadlock.com/archives/2010/08/20/linking-terms-to-a-specific-post"]]here[[/LINK]])
- Scribu suggests we can do this using his Posts 2 Posts plugin, but I´m not really sure how ([[LINK href="http://justintadlock.com/archives/2010/08/20/linking-terms-to-a-specific-post#comment-223781"]]here[[/LINK]])
- There´s also the WP_Rewrite that we can use to add filters
- Other options?

I hope a WP expert or someone who has already done this can tell me what would be the best way to set up this content structure. (No code, just the logic)


Answers (3)

2011-10-23

Milan Petrovic answers:

You are doing it all wrong. Custom post types have own archives implemented, you don't need pages for that, you only need WordPress 3.2, when archives are added.

I suggest you checkout my plugin GD Custom Posts and Taxonomies Tools Pro, that supports all that with custom rewrite rules and other things you might find useful:

http://www.dev4press.com/gd-custom-posts-and-taxonomies-tools/


Matt V comments:

I´m using page-templates because I´m actually publishing articles on these pages. For example, <em>example.com/hotels/5-stars/</em> would show an article about 5 star hotels and a list of CPT that have the taxonomy term "5-star".

Archives, as far as I know, are just a list of posts. They cannot be edited from the admin interface like pages.


Milan Petrovic comments:

Yeah, but with my plugin they can be filtered. So, if you have 10 hotels in your custom post type, and want do display archive with hotels that have 5-stars term, plugin allow creation of intersection archive that will filter all hotels and display those with 5-stars based on term.

If your stars are in custom taxonomy called 'rating' (for instance), and 'hotels' is custom post type, URL would look like:

example.com/hotels/rating/5-stars/

Everything is handled by archive template for custom post type (for your case named: archive-hotels.php or something like that). You can check this out in action on Dev4Press website:

Documentation: http://www.dev4press.com/documentation/ uses 2 custom taxonomies and custom post type, all filtering handled by WP and single theme templates. Just the same, Feature of The Day: http://www.dev4press.com/feature-of-the-day/. On both these, Products are actually taxonomies.

Milan

2011-10-23

Ivaylo Draganov answers:

Milan Petrovic is right - you don't need this:
<blockquote>This sub-page uses a custom page template that shows all hotels using the 5 star taxonomy term. The default taxonomy URL example.com/rating/5-stars would not exist</blockquote>

You can implement that with the native WP templates for custom terms. All need to do is create a file named <em>taxonomy-rating.php</em> in your theme folder. You can use your <em>archive.php</em> as a base for this new template and then modify the code to suit your needs. There's no need for any query_posts stuff - WP knows what to query when this template is loaded.

If you want to change the default 'ratings' URL base for your custom terms, then add a rewrite parameter* to your taxonomy registration function**:

'rewrite' => array( 'slug' => 'hotels', 'with_front' => false ),


* you can add the same parameter to your CPT registration function and have it rewrite the URLs to "hotels/my_hotel"
** re-save your permalink structure after adding/changing this parameter


Ivaylo Draganov comments:

<blockquote>I´m using page-templates because I´m actually publishing articles on these pages. For example, example.com/hotels/5-stars/ would show an article about 5 star hotels and a list of CPT that have the taxonomy term "5-star". </blockquote>

Then Justin Tadlock's tutorial is just the right thing for you. I read it when it got published and remember being quite impressed with it (and the new custom stuff in WP).


Matt V comments:

Thanks Ivaylo, looks like your answer covered the options. I´ll leave the question open for a few hours to see if anyone has anything to add.

2011-10-23

Luis Cordova answers:

This is the right direction as WP uses hooks and this is the component you are looking into modify
- There´s also the WP_Rewrite that we can use to add filters

the wp event pattern calls for extension this way, else you are hacking it the wrong way. this is then the standard, and should be the best way to do it.