WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this script to get the ancestors ID

//Get root ID
$post_id = $post->ID; 
$ancestors = get_post_ancestors($post_id);

I would like to make a redirection if it's empty to a specific page ID so no one could be on a page with no ancestors. How ?

share|improve this question
up vote 2 down vote accepted

The simple answer is wp_redirect($url);, but it's a tricky beast to use as you can only use it before any page output. So basically, what you have would need to be right at the top of your page

This example is making the assumption that you are using this in single.php? -

$post_id = $post->ID; 
$ancestors = get_post_ancestors($post_id);

if(empty($ancestors)) : // Not sure how $ancestors is returned, this assumes it's an array

See the Codex of wp_redirect() for more information.

share|improve this answer
wp_redirect($url); exit; – Max Yudin Mar 15 '13 at 15:29
Good point, have edited my answer. – David Gard Mar 15 '13 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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