1

Well the title gives it all away...

Here's the code:

$args = array(
"post_type" => "page",
"post_parent" => $cat, // runs through a loop of 5, 128, 130
"posts_per_page" => -1
);

print_r($args);

$postsLang = new WP_Query($args);

while ($postsLang->have_posts()) {
    $postsLang->the_post();
    $postID = get_the_ID();
    array_push($result, $postID);
}
wp_reset_query();

print_r($result);

Here the print_r outputs:

Array ( [post_type] => page [post_parent] => 5 [posts_per_page] => -1 ) 
Array ( [0] => 30 [1] => 23 ) 

Array ( [post_type] => page [post_parent] => 128 [posts_per_page] => -1 ) 
Array ( [0] => 30 [1] => 23 ) 

Array ( [post_type] => page [post_parent] => 130 [posts_per_page] => -1 ) 
Array ( [0] => 30 [1] => 23 ) 

So the $args go into the query correctly, but the outcome is always the same: the IDs of the children of the current page (ID = 5).

I can even set post_parent static to say 128 and still get the children of the current page.

If I remove the post_parent parameter I get all pages (as I should).

I'm having the same issue with get_posts, which makes me think that the syntax is right but that I might be misunderstanding the behaviour of the post_parent parameter?!

Any thoughts much appreciated!!

2
  • I think you should reset wp_reset_postdata(); instead of wp_reset_query();.
    – Robert hue
    Commented Nov 3, 2014 at 15:44
  • Hm, just checked, no changes. Even with resetting both.
    – sba222
    Commented Nov 3, 2014 at 16:30

1 Answer 1

1

post_parent returns only the child pages of the page whose ID is given as the value.

post_parent (int) - use page id to return only child pages. Set to 0 to return only top-level entries

To get just a specific page, rather use page_id or pagename or post__in (in conjunction with post_type=page.

I would suggest that you rather make use of get_pages which is specifically there to query pages.

Just a note here, wp_reset_query() is used in conjunction with query_posts. You should use wp_reset_postdata() with WP_Query

--

Here the solution from the comments below:

Polylang creates a new custom parameter "lang". This parameter appears to default to "", which is supposed to return posts with any value. Instead it returns posts with the current page's language value only (hence only children of current page ID = 5). As a fix I now manually loop through the available values for "lang" to get pages with all values for "lang". I'll check with the plugin author if this is a bug or caused by my environment.

7
  • Thanks for digging in! I do want to get the child pages. First of page 5, then of 128, then of 130. My parameters going into the query correctly, but the query seems to reset the post_parent parameter to the current page's ID.
    – sba222
    Commented Nov 3, 2014 at 16:38
  • Sorry, misread your question. Where exactly, on which template, are you using this. Is this on a page.php template. Are all custom queries reset. Try to add wp_reset_query() before your query Commented Nov 3, 2014 at 16:55
  • This is on a plain-ish template and page but I just realized that the issue goes away when I deactivate the polylang plugin! wp_reset_query() before the query does not help. Is there a way to safeguard the WP_Query from being messed with after the parameters have been set? I don't understand how at this point polylang (or anything else for that matter) can interfere with the query?!
    – sba222
    Commented Nov 3, 2014 at 17:23
  • Do you have any instance of pre_get_posts somewhere. Also, have cleared all caches and have you tried switching to a bundled theme. Post the complete page template. You can just remove any markup Commented Nov 3, 2014 at 17:26
  • What seems to be the cause Commented Nov 3, 2014 at 17:39

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.