php - Error 404 page not found after rewrite url from custom post type if exist other param
Get the solution ↓↓↓This is default link from custom post type:
domain.com/course/mern-stack-front-to-back-full-stack-react-redux-node-js
I am trying to create a custom rewrite rule if exist params is meta_id
domain.com/course/mern-stack-front-to-back-full-stack-react-redux-node-js?meta_id=1234
Should become this:
domain.com/course/mern-stack-front-to-back-full-stack-react-redux-node-js/lesson/1234
This is my code in functions.php
function custom_rewrite_tag() {
add_rewrite_tag( '%meta_id%', '([^&]+)' );
}
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule( '^lesson/([0-9]+)/?', 'index.php?post_type=course&meta_id=$matches[1]', 'top' );
}
add_action('init', 'custom_rewrite_rule', 10, 0);
Answer
Solution:
function custom_rewrite_tag() {
add_rewrite_tag( '%meta_id%', '([^&]+)' );
}
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule('^course/([^/]*)/lesson/([0-9]+)/?','index.php?post_type=course&name=$matches[1]&meta_id=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.