I got URL's like this:


This are pages / urls that doesnt exist (404) but we have custom post types (vacatures) with posts that have the same "id" in the slug:


These custom post types also have a custom field with the ID "P563328-18", dont know if this makes things easier.

I want to redirect people that visit this url:




I tried multiple things to get this done but I am a bit lost now.

If someone visits that redirect URL, in the database there must be searched for whether there is a custom post type with the same ID as the redirect URL. If there is a match go to the URL of the custom post type with the same ID.

I also tried to do it with a plug-in like this one: https://wordpress.org/plugins/wp-404-auto-redirect-to-similar-post/

But no luck there.

Has someone done something like this before?

I have already started this but get stuck every time.

$posts = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'vacatures',
    'meta_key' => 'vacature_id'

    foreach($posts as $post)
        $vacature_id = get_field('vacature_id');
        $vacature_id = strtolower($vacature_id);
        $location = get_site_url() . "/redirect/".$vacature_id."";
        $page_slug = $post->post_name;                    
if (strpos($page_slug, $vacature_id) !== false && strpos($location, $vacature_id) !== false) {
    wp_redirect( get_site_url() ."/vacatures/".$page_slug, 301 );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.