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". tour1 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 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_action('init', 'my_custom_rewrite_rule', 10, 0);

Finally, do not forget to flash the permalinks structure from your dashboard.

  • By 'flash the permalinks' do you mean press 'Save Changes' on Settings > Permalinks? – Clinton Oct 12 at 9:53
  • @Clinton, Yes exactly! – Rachid Chihabi Oct 12 at 11:30

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.