WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 23 months ago

#14502 new 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 (14)

comment:1 @vteixeira5 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/

comment:2 @mikeschinkel5 years ago

  • Cc mikeschinkel@… added

comment:3 follow-up: @scribu4 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.

comment:4 in reply to: ↑ 3 @mikeschinkel4 years ago

Replying to scribu:

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

Nice!

comment:5 @vteixeira4 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.

comment:6 @scribu4 years ago

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

comment:7 @scribu4 years ago

Marked as dup: #16554

comment:8 @ptahdunbar4 years ago

  • Cc trac@… added

comment:9 @johnnyb3 years ago

  • Cc johnbeales@… added

comment:10 @wpsmith3 years ago

  • Cc travis@… added

comment:11 @dd323 years ago

  • Component changed from Permalinks to Rewrite Rules

comment:12 @retlehs2 years ago

  • Cc retlehs added

comment:13 @mboynes23 months 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

comment:14 @mboynes23 months ago

  • Cc mboynes@… added
Note: See TracTickets for help on using tickets.