Redirect 404 page with ID in slug to associated page with same ID in slug


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:

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 );

, , , , user2812779 4 years 2019-11-06T08:30:45-05:00 0 Answers 68 views 0

Leave an answer