WordPress.org

Make WordPress Core

Opened 11 years ago

Last modified 2 years ago

#14502 reopened enhancement

Enable /post-type/taxonomy/term/ permalinks

Reported by: scribu Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords:
Focuses: Cc:

Description

After we get /post-type/ handled (see #13818), it would be nice if we also had /post-type/taxonomy/term/ mapped to ?post_type=post-type&taxonomy=term

Change History (17)

#1 @vteixeira
11 years ago

Hi, thanks for creating this ticket.

That's exactly what's missing.

I just made a quick search and found that a lot of people are missing it.

See the comments of this post: http://www.cmurrayconsulting.com/software/wordpress-custom-post-type-archives/

#2 @mikeschinkel
11 years ago

  • Cc mikeschinkel@… added

#3 follow-up: @scribu
11 years ago

Turns out this is almost doable with WP 3.1's post type archives:

function shape_init() {
	register_taxonomy( 'shape-type', 'shape', array(
		'rewrite' => array( 'slug' => 'shapes/type' ),
		'label' => 'Shape Types'
	) );

	register_post_type( 'shape', array(
		'public' => true,
		'has_archive' => 'shapes',
		'label' => 'Shapes'
	) );
}
add_action('init', 'shape_init');

The URLs would look like this:

/shapes/             # all shapes
/shapes/type/3d/     # 3d shapes only
/shapes/triangle/    # a singular shape

Another variation:

function shape_init() {
	register_post_type( 'shape', array(
		'has_archive' => 'shapes',
		'rewrite' => array( 'slug' => 'shapes/single' ),
		'public' => true,
		'label' => 'Shapes'
	) );

	register_taxonomy( 'shape-type', 'shape', array(
		'rewrite' => array( 'slug' => 'shapes' ),
		'label' => 'Shape Types'
	) );
}
add_action('init', 'shape_init');

Note: the post type has to be registered before the taxonomy.

Now, the URLs would be:

/shapes/                  # all shapes
/shapes/3d/               # 3d shapes only
/shapes/single/triangle/  # a singular shape

In both examples, the taxonomy is tied to a single post type.

#4 in reply to: ↑ 3 @mikeschinkel
11 years ago

Replying to scribu:

Turns out this is almost doable with WP 3.1's post type archives:...

Nice!

#5 @vteixeira
11 years ago

Hi Scribu, this is not a solution to the problem.

Simply because you are hardcoding the post type on the taxonomy slug, so it doesn't work.

This functionality is just needed because we need to have one taxonomy registered to different post types and we need to be able to filter those taxonomies by the post type on the url.

Your example works great for one taxonomy registered to one post type, but that was already possible...

The use case is:

Suppose I have two post types: Cars and Airplanes.
And one taxonomy: Color (registered for both post types).

I need to be able to call: 'site.com/cars/color/grey' and also 'site.com/airplanes/color/grey'.

And it should work as anyone would expect: filtering the taxonomy by the post type, as if I was calling 'site.com/?post_type=cars&color=grey' or 'site.com/?post_type=airplanes&color=grey'.

I'm not able to do this right now.

#6 @scribu
11 years ago

Yes, I know. That's why I said "almost".

#7 @scribu
11 years ago

Marked as dup: #16554

#8 @ptahdunbar
10 years ago

  • Cc trac@… added

#9 @johnnyb
9 years ago

  • Cc johnbeales@… added

#10 @wpsmith
9 years ago

  • Cc travis@… added

#11 @dd32
9 years ago

  • Component changed from Permalinks to Rewrite Rules

#12 @retlehs
9 years ago

  • Cc retlehs added

#13 @mboynes
8 years ago

Stumbled across this old ticket and figured I might be able to lend a hand.

I think that this is doable with the current Rewrite API. You'd need to first add %post_type% as a rewrite tag, then use that in the taxonomy slug. You'd also want to prevent the taxonomy permastruct from creating rewrites for just %post_type%, so pass 'walk_dirs' => false in the rewrite array.

function shape_init() {
	add_rewrite_tag( '%post_type%', '([^/]+)' );
	register_taxonomy( 'shape-type', 'shape', array(
		'rewrite' => array(
			'slug' => '%post_type%/type',
			'walk_dirs' => false
		),
		'label' => 'Shape Types'
	) );

	register_post_type( 'shape', array(
		'public' => true,
		'has_archive' => true,
		'label' => 'Shapes'
	) );
}
add_action( 'init', 'shape_init' );

The only caveats that I can see are:

  1. Order is important. Here, the taxonomy must be registered before the post type.
  2. If you have a single shape with a slug of "type" it will cause some conflicts; it's post pagination would break, feeds would break, etc., though the "single" view of /shape/type/ would work

#14 @mboynes
8 years ago

  • Cc mboynes@… added

#17 @antwortzeit
3 years ago

  • Status changed from new to reopened

I think we should leave the ticket open. In our work this would often be a big help if we had correct archive links for taxonomies based on post types. At the same time I can't offer any help because I don't have enough expertise with permalinks.

#18 @SergeyBiryukov
3 years ago

  • Milestone set to Future Release
Note: See TracTickets for help on using tickets.