WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 5 weeks ago

#21374 new enhancement

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

Reported by: maorb Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.4.1
Component: Rewrite Rules Keywords:
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 (10)

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

comment:2 @sirzooro3 years ago

  • Cc sirzooro added

+1 for this

comment:3 @maorb3 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

comment:4 @mikeschinkel3 years ago

  • Cc mikeschinkel@… added

+1

comment:5 @greenshady3 years ago

  • Cc justin@… added

comment:6 @netweblogic3 years ago

  • Cc msykes@… added

a very in demand feature, often get asked this

comment:7 @Ipstenu13 months ago

#27930 was marked as a duplicate.

comment:9 @SergeyBiryukov5 weeks ago

#32043 was marked as a duplicate.

comment:10 @gmariani4055 weeks ago

Add my vote for this

Note: See TracTickets for help on using tickets.