2

I have a hierarchy that I need to get data from. It's the child (by slug name) that I need to get while already having knowledge of the parents.

Highest Known Parent Page (slug : 'parent')
|_ Child 1 (slug : 'child-1')
   |_ Page 1 (slug : 'page-1')
   |_ Page 2 (slug : 'page-2')
|_ Child 2 (slug : 'child-2')
   |_ Page 1 (slug : 'page-1')
   |_ Page 2 (slug : 'page-2')
|_ Child 3 (slug : 'child-3')
   |_ Page 1 (slug : 'page-1')
   |_ Page 2 (slug : 'page-2')

Ideally I'm trying to have a result that returns "page-2" consistently, and I am already working within a loop where I know "child-1", "child-2", "child-3", etc.I am just not sure how to query the third level. Any ideas?

I'm probably over-thinking this, as there's probably an easy way to get an ID of a page from a slug, when you already know the parent. Of course, I'll need to script contingencies in case "page-2" doesn't exist for some reason, but I first need to get the right data.

UPDATE:

Here's the code I'm working with right now. I know models. I know children of models. I'm trying to get each and every one of a certain child underneath all model children. For example, a "specifications" page. So I'm hoping to have like 5-6 different "specifications" pages returned to me...so I can easily link to them (dynamically).

<?php
// get models id
$models = get_page_by_path( 'models' );

// get children of models page
$args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $models->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
);
$children_posts = new WP_Query( $args );
?>
1

You should use the pagename parameter for WP_Query along with post_parent. Limit your query to one post using posts_per_page.

$parent_id = get_the_ID();
$args = array(
    'post_type'      => 'page'
    'pagename'       => 'page-2',
    'post_parent'    => $parent_id,
    'posts_per_page' => 1
);
$posts = get_posts( $args );
$child = isset( $posts[0] ) ? $posts[0] : false;
if( $child ){
    //Do Something
}

This, in theory, should render your desired post by slug using the parent ID.

0

Have you already looked at get_page_children? you need to pass the id of the child page and the $children_posts to it and it will return all childpages of it...

what you need is: child-1->ID and an array with child-1, child-2 and so on

0
<?php
// get models id
$models = get_page_by_path( 'models' );

// get children of models page
$args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $models->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
);

$children_posts = new WP_Query( $args );
-1

i hope this help you, you need to get ID of your parent's page and start the loop like this

<?php

    $args = array( 'posts_per_page' => -1, 'post_type' => 'page', 'post_parent' => 52, 'orderby' => 'modified');
    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();

            // Get next child
            $args2 = array( 'posts_per_page' => -1, 'post_type' => 'page', 'post_parent' => get_the_ID(), 'orderby' => 'modified');
            $loop2 = new WP_Query( $args );

            while ( $loop->have_posts() ) : $loop->the_post();

                // Your code

?>
  • Thanks for your reply! Unfortunately, that doesn't get me to the third level by knowing the "page-2" slug. I'm already in a loop at the child level. It's the third level that I am not sure about. I'm hoping to have each and every one of the "page-2" pages returned to me that's at that level of depth. – Will Ashworth Jan 3 '14 at 17:16
  • use again a loop :p i have edit my post – Snail Jan 3 '14 at 17:19
  • That's still not quite what we're after. Unless I'm mistaken, you're selecting all children. Whereas I want a specific child at the third level (by slug). I suppose I could work with what you've posted and do an if() inside the "your code" section, but that seems unclean to me. Why select tons and tons of children, only to output a few? Wouldn't I instead want to select what's actually expected to be output? (ie, the specs pages underneath a child). I don't need every single page under a child. I need a specific, single, page under each child. Thanks for trying to help! :) – Will Ashworth Jan 3 '14 at 17:24
  • Ok, i understand now, so ... sorry i have no idea, i think loop is the simple way but heavy – Snail Jan 3 '14 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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