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'm using WordPress to organize documentation for a software product I'm supporting and have hit a bit of a snag. I have multiple FAQ documents (saved as pages) that I need to be separate pages but that I want to be able aggregate them into one FAQ master list.

To do this, I installed the "Ninja Page Categories and Tags" plugin so that I could tag each separate page with 'faq'. What I wanted to do then was query for all pages with that tag and then aggregate them into 3 lists, depending on the function of the FAQ: general, instructors, or students. Each result would be sorted based on the ancestor of the individual page.

This is the code I developed:

$instructors = array();
$student = array();
$general = array();

$postsByTag = get_posts('tag=faq&post_type=page&numberposts=0');

foreach($postsByTag as $post) {


    $title = $post->post_title;
    $title = str_replace("FAQ: ", "", $title);

    $id = $post->ID;

    $ancestors = get_post_ancestors($post);

    if($post->ancestors && in_array( '386', $post->ancestors)) {
        $instructors[] = "<li><a href='". get_permalink()."'>$title</a></li>";
    } else if(in_array( '384', $post->ancestors)) {
        $students[] ="<li><a href='". get_permalink()."'>$title</a></li>";
    } else {
        $general[] = "<li><a href='". get_permalink()."'>$title</a></li>";


The code returns all of the pages tagged with 'faq' but it sorts everything into the "general" category because it seems that get_post_ancestors returns empty, even though I know that's not the case.

What am I doing wrong here? Can anybody help me out? Thanks!

share|improve this question
what are you actually doing with the array $ancestors ? it is not used in your code. there is also no field ancestors in the $post table. – Michael Oct 10 '11 at 18:38
The $post->ancestors technique is something I found on the web somewhere that has worked for me in the past. I used $ancestors to print_r the array to see what was inside (nothing) and should have removed it from this code. However, when changing $post->ancestors to $ancestors the result is the same. – mcleodm3 Oct 10 '11 at 18:53
what if you try get_post_ancestors($id); ? – Michael Oct 10 '11 at 19:13
try $ancestors = get_ancestors($id,'page'); codex.wordpress.org/Function_Reference/get_ancestors – Michael Oct 10 '11 at 20:41
@Michael please move your code to an answer so this doesn't haunt site as unanswered. – Rarst Oct 12 '11 at 21:29


$ancestors = get_ancestors($id,'page');


share|improve this answer

From 2.8 to 3.4 get_post_ancestors() could incorrectly give an empty result.

It's supposed to be fixed in 3.5 so it should be safe to use now.

share|improve this answer

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.