[wp-trac] [WordPress Trac] #21374: Add core support for letting custom permalink for diffeent post types
WordPress Trac
wp-trac at lists.automattic.com
Wed Jul 25 14:42:15 UTC 2012
#21374: Add core support for letting custom permalink for diffeent post types
---------------------------+-----------------------------
Reporter: maorb | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Rewrite Rules | Version: 3.4.1
Severity: normal | Keywords:
---------------------------+-----------------------------
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.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/21374>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list