I am building a single author blog that will include multiple categories. One main category is “Movie Reviews.”
The author will leave a short review every time they watch a movie – even if they’ve seen the movie in the past and reviewed it on their site in the past.
For the “Movie Reviews” category (only this category), I need to setup a permalink structure such as:
- /%category%/%postname%-%day%%monthnum%%year%/
- /movie-review/the-hateful-eight-01192016/
This will give a unique URL to each review of the same movie.
The rest of the categories will simply use /%postname%/
I am 99% positive that I did this exact thing a few years ago, but that site is no longer active, I don’t have anything about the process in my notes, and I cannot seem to find any direction via Google search, WordPress.org forums, or WordPress Answers.
2 Answers
I am not sure if this is the best solution or not, but it works:
function movie_review_permalink( $url, $post, $leavename ) {
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->slug == "test" ) { //change 'test' to your category slug
$date=date_create($post->post_date);
$my_date = date_format($date,"dmY");
$url= trailingslashit( home_url("https://wordpress.stackexchange.com/". $category[0]->slug ."https://wordpress.stackexchange.com/". $post->post_name .'-'. $my_date ."https://wordpress.stackexchange.com/" ) );
}
return $url;
}
add_filter( 'post_link', 'movie_review_permalink', 10, 3 );
Above code will make your post permalink for category test to http://wpHomeURL/test/post-name-ddmmyyyy
structure.
Now you will need to add rewrite rule o make this work.
function movie_review_rewrite_rules( $wp_rewrite ) {
$new_rules['^test/([^/]+)-([0-9]+)/?'] = 'index.php?name=$matches[1]'; //change 'test' to your category slug
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
add_action('generate_rewrite_rules', 'movie_review_rewrite_rules');
Hope this helps!