0

I have a custom post type (portfolio) and I have to redirect a certain page (of this custom post type) to another page.

when I look at my custom post type page:

enter image description here

So I wrote the code as follow to redirect the above 'portfolio' page :

if (is_singular('2024')) {
   wp_redirect('http://mysitedomain.com/../rugby/');
}

but when I visit to the page (ID=2024) in front end it does not redirect to the page http://mysitedomain.com/../rugby/ why is that?

2
  • you should add exit after.
    – RRikesh
    Mar 30, 2016 at 13:42
  • exit isn't necessary after redirection!
    – Sumit
    Mar 30, 2016 at 15:31

1 Answer 1

5

is_singular() only accept post_type, use is_single('2024') for specific post.

if (is_single(2024)) {
     wp_redirect('http://mysitedomain.com/../rugby/');
     exit();
}

Update:

Also wp_redirect() does not work after headers are sent. Please make sure you are redirecting before headers. So you can hook this function on template_redirect and put this in functions.php

function redirect_custom_page() {
   if (is_single('2024')) {
        wp_redirect('http://mysitedomain.com/../rugby/');
        exit();
   }
}
add_action('template_redirect', 'redirect_custom_page');
6
  • yes.. I want to redirect that specific post to another page but when I visiti to that specific post it does nothing.. I mean it is not redirecting.. Mar 30, 2016 at 11:23
  • and I tried your answer too.. but still not working Mar 30, 2016 at 11:28
  • check updated version, this should work
    – Nefro
    Mar 30, 2016 at 11:45
  • Please explain why downvote on this answer?
    – Sumit
    Mar 30, 2016 at 15:12
  • @Nefro OP shows the URL - the circled part is most likely not the post slug but the post ID. Maybe change it to is_single( 2024 ) so that WP interprets it as an INT and tries to look for the ID and not the post-slug.
    – Howdy_McGee
    Mar 30, 2016 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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