只需使用parse_request
钩无需自定义重写规则和标记。
因此,请删除此选项(您应该刷新重写规则,只需访问永久链接设置页面即可):
add_action( \'init\', \'vacature_rewrite_rule\' );
function vacature_rewrite_rule() {
...
}
然后使用此修改后的函数执行重定向:
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;
}
}