$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom permalink not working and showing 404 page not error|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom permalink not working and showing 404 page not error

matteradmin12PV0评论

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 1
Add a comment  | 

2 Answers 2

Reset to default 1

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_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 ));
}
Post a comment

comment list (0)

  1. No comments so far