WordPress.org

Ready to get started?Download WordPress

Forums

Problem in get_pages with child_of and number (5 posts)

  1. eprieto
    Member
    Posted 1 year ago #

    I have been reading the code of the function get_pages in the file wp-includes/post.php, and I found that get_pages first get all the pages that comply with the arguments. If I limit the query to some number of pages like 2, the size of the return cant be grater than 2.
    Then if I setup child_of with an ID, executes get_page_children with the array of pages obtained before in the second parameter.
    Thats the problem! Only get the childs of a page from 2 pages, not from all pages that comply with the query.

    If I use parent instead of child_of the return its ok, but ins't the same, we know the difference between both parameters.

    If Im wrong, please let me know.

    Thanks

  2. bcworkz
    Member
    Posted 1 year ago #

    I'm not sure exactly what you're asking, but you make an interesting observation that is not documented well. In plain English, the terms are the same. If I'm a page and my parent page is 'Father', I am also a child_of 'Father'. How the arguments are used in get_pages() is vastly different.

    The parent argument is limited to a single generation. The child_of argument spans many generations. The page 'Grandfather' is not my parent, but I am a child_of 'Grandfather'. This much is documented.

    What's not documented is only the parent argument becomes part of the SQL query. If the number argument is 2, the query will return 2 pages who's parent field matches the argument.

    The child_of argument is not part of the query. It is fed to get_page_children() along with the query results within the get_pages() function. Thus the child_of argument may return less than the number argument, depending what was returned from the query. I will add a note regarding this to the documentation.

    However, if you provide both the parent and child_of arguments, and they are the same (which is the only proper way to use the parent argument according to documentation), the get_page_children() portion will return all of the query results, since they all have the same parent and are thus all children of that parent. So I don't really see the need for the child_of argument at all when using the parent argument. I will not change the documentation on this aspect without further investigation though.

  3. eprieto
    Member
    Posted 1 year ago #

    In my project I need to use child_of not parent paging by 2 items , thats why I start this post.
    I have tested using both parameters and analized the post.php code before writing this.
    Isn't a bug? or a bad design of the function? I thing so.

    Thanks for the reply.

  4. bcworkz
    Member
    Posted 1 year ago #

    You could call it a bug since it does not return what you requested. I wouldn't call it a bug myself, but I agree it is a poor design. I suppose the issue is there is no simple way to query multi generational pages. (Just guessing, I don't know SQL that well)

    The real issue is how to get the functionality you need. The obvious but poor answer is query all child_of results and handle the paging in your own code. Have you looked at get_posts()? I wonder if that would work differently if you passed 'post_type'=>'page'.

    Or if you know SQL well enough, construct your own query (if it's even possible) and use some query filter or $wpdb methods to implement it.

    BTW, I did investigate the parent parameter and the child_of requirement, not that it helps you any. It turns out the real requirement is 'hierarchical' must be set to 0 when using 'parent', the 'child_of' parameter is more of a hacky alternative to 'hierarchical'=>0. I've re-written the Codex description to reflect this.

  5. eprieto
    Member
    Posted 1 year ago #

    I have solved using SQL before I wrote the post.
    My motivation was improve the functionality of get_pages, I will rewrite the this function to work as it should.

    Thanks for all.

Topic Closed

This topic has been closed to new replies.

About this Topic