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

    setup_postdata($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!

link|improve this question

40% accept rate
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
1  
try $ancestors = get_ancestors($id,'page'); codex.wordpress.org/Function_Reference/get_ancestors – Michael Oct 10 '11 at 20:41
1  
@Michael please move your code to an answer so this doesn't haunt site as unanswered. – Rarst Oct 12 '11 at 21:29
show 4 more comments
feedback

1 Answer

use:

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

get_ancestors()

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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