I have created 1 custom post and some custom fields.
- Custom post type: Tour
- Custom field: tour_id
Now I want to customize permalink link structure, want to append "tour_id" value in link. I'm using below code.
add_action('init', 'pub_rewrite_rules');
function pub_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( '%pstname%', '([^/]+)', 'post_type=tour&name=');
$wp_rewrite->add_rewrite_tag( '%tourid%', '([^/]{4})', 'tourid=');
$wp_rewrite->add_permastruct('tour', '/tour/%pstname%/%tourid%', array( 'walk_dirs' => false ));
}
function pub_permalink($permalink, $post, $leavename) {
if((false !==strpos( $permalink, '%tourid%') ) && get_post_type($post)=='tour') {
$publicationtype = get_post_meta($post->ID, 'tour_id',true);
$rewritecode = array('%pstname%','%tourid%');
$rewritereplace = array($post->post_name,$publicationtype);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
I'm getting correct link "https://tourjourney/tour/tour-1/1000/" here, "tour" is custom post, "tour-1" is post title and "1000" is tour_id, but while viewing the post i'm getting "404 File Not Found error". I have done above code in function.php file of theme. I have checked .htaccess, it's proper.
I guess the problem is in add_permastruct or m'I missing anything in code.
I have created 1 custom post and some custom fields.
- Custom post type: Tour
- Custom field: tour_id
Now I want to customize permalink link structure, want to append "tour_id" value in link. I'm using below code.
add_action('init', 'pub_rewrite_rules');
function pub_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( '%pstname%', '([^/]+)', 'post_type=tour&name=');
$wp_rewrite->add_rewrite_tag( '%tourid%', '([^/]{4})', 'tourid=');
$wp_rewrite->add_permastruct('tour', '/tour/%pstname%/%tourid%', array( 'walk_dirs' => false ));
}
function pub_permalink($permalink, $post, $leavename) {
if((false !==strpos( $permalink, '%tourid%') ) && get_post_type($post)=='tour') {
$publicationtype = get_post_meta($post->ID, 'tour_id',true);
$rewritecode = array('%pstname%','%tourid%');
$rewritereplace = array($post->post_name,$publicationtype);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
I'm getting correct link "https://tourjourney/tour/tour-1/1000/" here, "tour" is custom post, "tour-1" is post title and "1000" is tour_id, but while viewing the post i'm getting "404 File Not Found error". I have done above code in function.php file of theme. I have checked .htaccess, it's proper.
I guess the problem is in add_permastruct or m'I missing anything in code.
Share Improve this question asked Oct 11, 2018 at 10:11 AshAsh 12 Answers
Reset to default 1I had the same technical need as you, and I have did this(code below) to get it work :
function my_custom_rewrite_tag() {
add_rewrite_tag('%xxx%', '([^&]+)'); //change the regex to your needs
add_rewrite_tag('%yyy%', '([^&]+)'); //change the regex to your needs
}
add_action('init', 'my_custom_rewrite_tag', 10, 0);
function my_custom_rewrite_rule() {
add_rewrite_rule('^tour/([^/]*)/([^/]*)/?','index.php?pagename=tour&xxx=$matches[1]&yyy=$matches[2]','top');
}
add_action('init', 'my_custom_rewrite_rule', 10, 0);
Finally, do not forget to flash the permalinks structure from your dashboard.
I just modify {4} => + sign
my all posts is working proper.
{4} means it's taking only 4 digit of tourid
rest of code is same.
add_action('init', 'pub_rewrite_rules');
function pub_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( '%pstname%', '([^/]+)', 'post_type=tour&name=');
$wp_rewrite->add_rewrite_tag( '%tourid%', '([^/]+)', 'tourid=');
$wp_rewrite->add_permastruct('tour', '/tour/%pstname%/%tourid%', array( 'walk_dirs' => false ));
}