1

is there any way to get the redirect url in PHP when all you have is the origin url? For example: - You create an article that has a slug of test-redirect. - Inside the article editing screen you change that slug to test-redirect-new If all you have is http://example.com/test-redirect, is there some function in which you can pass http://example.com/test-redirect and get back http://example.com/test-redirect-new? Is there any way to access it via wp_remote_get? Thanks in advance for any help you can provide.

1

You can simply write your own function this way:

function get_old_link($url) {
        global $wpdb;

        // get only url path 
        $parse = parse_url($url);
        $path = trim($parse['path'], "/");

        // get last part as slug
        $arr = explode("/", $path);
        $slug = end($arr);

        // find post id by meta_key and meta_value
        $row = $wpdb->get_row("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_old_slug' AND meta_value = '{$slug}'");

        if(!empty($row->post_id))
            return get_permalink($row->post_id);
}

Note: it is possible to make this function better. I only want to show you the direction.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.