Make WordPress Core

Opened 12 years ago

Last modified 6 years ago

#21374 new enhancement

Add core support for letting custom permalink structure for different post types

Reported by: maorb's profile maorb Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Rewrite Rules Keywords: needs-patch
Focuses: Cc:

Description

By default, custom post types uses only the %postname% permalink structure (if using pretty links and not default query variables).
Currently there is no native way to easily have different permastructs for different CPT.

This features should be added to the register_post_type() function, letting different permalink structure be defined to each post_type being registered.

I sugget adding another new key to the rewrite array that can be passed to the register_post_type() function.

i.e. -

register_post_type('event',array(
......
'rewrite' => array('slug' => 'events', 
	   'permastruct' => '%year%/%monthnum%/%event%'
	),
.....
));

The register_post_type function should be changed, a suggestion for such a change may be that instead of this line in the function - (post.php line#1075)

add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite );

This example could be used -

if (  $args->rewrite['permastruct'] )	{			
			$perma_structure = $args->rewrite['permastruct'];			
			$wp_rewrite->add_rewrite_tag("%{$post_type}%", '([^/]+)', "{$post_type}=");
			$wp_rewrite->add_permastruct($post_type, $archive_slug.'/'.$perma_structure, false);	
		}
		else	{
			add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite );			
		}

In order that the structure can interpret the permastruct tags, I added also this function to filter post_type_link, it is adapted from get_permalink function in wp-includes/link-template.php -

add_filter('post_type_link', 'tc_permalink', 10, 3);	
function tc_permalink($permalink, $post_id, $leavename) {
	$post = get_post($post_id);
	$rewritecode = array(
		'%year%',
		'%monthnum%',
		'%day%',
		'%hour%',
		'%minute%',
		'%second%',
		$leavename? '' : '%postname%',
		'%post_id%',
		'%category%',
		'%author%',
		$leavename? '' : '%pagename%',
	);
 
	if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
		$unixtime = strtotime($post->post_date);
 
		$category = '';
		if ( strpos($permalink, '%category%') !== false ) {
			$cats = get_the_category($post->ID);
			if ( $cats ) {
				usort($cats, '_usort_terms_by_ID'); // order by ID
				$category = $cats[0]->slug;
				if ( $parent = $cats[0]->parent )
					$category = get_category_parents($parent, false, '/', true) . $category;
			}
			// show default category in permalinks, without
			// having to assign it explicitly
			if ( empty($category) ) {
				$default_category = get_category( get_option( 'default_category' ) );
				$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
			}
		}
 
		$author = '';
		if ( strpos($permalink, '%author%') !== false ) {
			$authordata = get_userdata($post->post_author);
			$author = $authordata->user_nicename;
		}
 
		$date = explode(" ",date('Y m d H i s', $unixtime));
		$rewritereplace =
		array(
			$date[0],
			$date[1],
			$date[2],
			$date[3],
			$date[4],
			$date[5],
			$post->post_name,
			$post->ID,
			$category,
			$author,
			$post->post_name,
		);
		$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
	} else { // if they're not using the fancy permalink option
	}
	return $permalink;
}

On a basic check this seems to be working, but ofcourse needed to be more deubgged.. I will be happy to here if you think such a feature should be added to core.

Change History (14)

#1 @maorb
12 years ago

  • Summary changed from Add core support for letting custom permalink for diffeent post types to Add core support for letting custom permalink for different post types

#2 @sirzooro
12 years ago

  • Cc sirzooro added

+1 for this

#3 @maorb
12 years ago

  • Summary changed from Add core support for letting custom permalink for different post types to Add core support for letting custom permalink structure for different post types

#4 @mikeschinkel
12 years ago

  • Cc mikeschinkel@… added

+1

#5 @greenshady
12 years ago

  • Cc justin@… added

#6 @netweblogic
12 years ago

  • Cc msykes@… added

a very in demand feature, often get asked this

#7 @Ipstenu
11 years ago

#27930 was marked as a duplicate.

#9 @SergeyBiryukov
10 years ago

#32043 was marked as a duplicate.

#10 @gmariani405
10 years ago

Add my vote for this

#11 @johnbillion
10 years ago

#32595 was marked as a duplicate.

#12 @johnbillion
10 years ago

  • Keywords needs-patch added
  • Version changed from 3.4.1 to 2.9

Blatent self-promotion: my Extended CPTs library provides this functionality. I would support adding this to core.

This ticket was mentioned in Slack in #core by zoker. View the logs.


9 years ago

Note: See TracTickets for help on using tickets.