I found this post (Create additional short URL with custom field and 301 redirect) and tried to do this with a custom post type I created but it aint working and I dont know what I am doing wrong in this case. I got URL's like this:
example/vacatures/junior-adviseur-kabels-en-leidingen-p565148-6/
and I want to create a short URL like this:
example/redirect/p565148-6/
And make a 301 redirect to
example/vacatures/junior-adviseur-kabels-en-leidingen-p565148-6/
I got a custom post type called "Vacatures" and a custom field "vacature_id".
I allready got this but that doesn't do anything.
add_action( 'init', 'vacature_rewrite_rule' );
function vacature_rewrite_rule() {
add_rewrite_tag( '%vacature_id%', '([a-zA-Z0-9]+)' );
add_rewrite_rule(
'^redirect([a-zA-Z0-9]+)?',
'index.php?vacature_id=$matches[1]',
'top'
);
}
add_action( 'parse_request', 'wpd_catch_vacature_requests' );
function wpd_catch_vacature_requests( $query ) {
if( ! is_admin() && isset( $query->query_vars['vacature_id'] ) ){
$the_post = new WP_Query(
array(
'post_type' => 'vacatures',
'meta_key' => 'vacature_id',
'meta_value' => $query->query_vars['vacature_id']
)
);
if( $the_post->have_posts() ){
wp_redirect( get_permalink( $the_post->post->ID ) );
} else {
wp_redirect( home_url() );
}
}
}
I found this post (Create additional short URL with custom field and 301 redirect) and tried to do this with a custom post type I created but it aint working and I dont know what I am doing wrong in this case. I got URL's like this:
example/vacatures/junior-adviseur-kabels-en-leidingen-p565148-6/
and I want to create a short URL like this:
example/redirect/p565148-6/
And make a 301 redirect to
example/vacatures/junior-adviseur-kabels-en-leidingen-p565148-6/
I got a custom post type called "Vacatures" and a custom field "vacature_id".
I allready got this but that doesn't do anything.
add_action( 'init', 'vacature_rewrite_rule' );
function vacature_rewrite_rule() {
add_rewrite_tag( '%vacature_id%', '([a-zA-Z0-9]+)' );
add_rewrite_rule(
'^redirect([a-zA-Z0-9]+)?',
'index.php?vacature_id=$matches[1]',
'top'
);
}
add_action( 'parse_request', 'wpd_catch_vacature_requests' );
function wpd_catch_vacature_requests( $query ) {
if( ! is_admin() && isset( $query->query_vars['vacature_id'] ) ){
$the_post = new WP_Query(
array(
'post_type' => 'vacatures',
'meta_key' => 'vacature_id',
'meta_value' => $query->query_vars['vacature_id']
)
);
if( $the_post->have_posts() ){
wp_redirect( get_permalink( $the_post->post->ID ) );
} else {
wp_redirect( home_url() );
}
}
}
Share
Improve this question
asked Nov 11, 2019 at 9:39
user2812779user2812779
1472 silver badges9 bronze badges
1 Answer
Reset to default 1What you're trying to do, can be achived by simply using the parse_request
hook; no need for the custom rewrite rule and tag.
So remove this (and you should flush the rewrite rules — just visit the permalink settings page):
add_action( 'init', 'vacature_rewrite_rule' ); function vacature_rewrite_rule() { ... }
Then use this modified function which performs the redirection:
add_action( 'parse_request', 'wpd_catch_vacature_requests' ); function wpd_catch_vacature_requests( $query ) { if( ! is_admin() && preg_match( '#^redirect/([\w\-]+)$#', $query->request, $matches ) ){ $posts = get_posts( array( 'post_type' => 'vacatures', 'meta_key' => 'vacature_id', 'meta_value' => $matches[1], 'fields' => 'ids' ) ); // Redirect to the post. if( ! empty( $posts ) ){ // a valid post found wp_redirect( get_permalink( $posts[0] ) ); // Or otherwise, the homepage. Or you can remove this and a 404 page would be shown. } else { wp_redirect( home_url() ); } exit; } }