Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Punal Chotrani
Punal Chotrani
8,817 Points

How to get ids of pages of the same name under different parent to do a query loop in wordpress?

This is what i usually do

$page = get_page_by_title($title);
return $page->ID;

But unfortunately i have two pages of about us under different parent pages. I need to get the ID of the specific page 'About' which has a specific parent.

6 Answers

Casey Ydenberg
Casey Ydenberg
15,622 Points

There isn't going to be a bullet-proof solution because two pages can have the same title, even under the same parent. It's only the slug that you can count on being unique.

If you can use the slug you can do something like

$query = new WP_Query( array( 'pagename' => 'parent-slug/about' ) );
$id = $query->posts[0]->ID

If you have to query by title there's no default support - you'll have to query by keyword (which searches both the title and content) and then loop through them and filter.

WP_Query is much easier to customize than functions like get_page_by_title; I probably refer more here than any other part of the codex.

Punal Chotrani
Punal Chotrani
8,817 Points

Hi Casey,

This is a good solution, but i would like to use the ID instead of the slug, as we have editors that keep changing the title and slug. I believe , what will not change is the ID, which is why i would like to use the ID

I'm able to the the ID of 'Events' which is the parent of 'About'

How do i get the ID of the 'About' page by title?

Punal Chotrani
Punal Chotrani
8,817 Points

I have tried this method, and i'm currently getting 'null'.

Not sure at this point what to do.

You can find the page ID in the admin area under Pages. If you hover over the page name you can find the page ID in a long URL.

Punal Chotrani
Punal Chotrani
8,817 Points

Hi Rune,

I need the ID's of the page to be returned so i can do a specific loop with WP_Query, and add the ID into the arguments.

Hi Punal, maybe something like this is what you are looking for:

$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

// Get the page as an Object

$portfolio = get_page_by_title('Portfolio');

// Filter through all pages and find Portfolio's children

$portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );

If you want to get the ID for a Page Object you can do it like this: $portfolio->ID;

Punal Chotrani
Punal Chotrani
8,817 Points

Not able to run your code, it would break the page using

$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

I tried something like this

$page_title = get_page_by_title('Events');
$parent = $page_title->ID;

$query = new WP_Query( array(
 'pagename' => 'events/gallery'
        ) );                       
$id = $query->posts[0]->ID;
var_dump($id);

But all i'm getting is 'null'.

Any other suggestions, on how could we get the ID of events, just by using the name?

Sorry, my mistake. Previous code break because forget to make a new WP_Query. Check if it echo out the right pages, then you can get the ID by using $portfolio_children[0]->ID The code should look like this:

<?php // Set up the objects needed

$my_wp_query = new WP_Query();

$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

// Get the page as an Object

$portfolio = get_page_by_title('Portfolio');

// Filter through all pages and find Portfolio's children

$portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );

// echo what we get back from WP to the browser echo '<pre>' . print_r( $portfolio_children, true ) . '</pre>'; ?>

Punal Chotrani
Punal Chotrani
8,817 Points

Hi Rune,

Very strange. It's still returning 'null'.

I wonder of we are targeting the parent page in the wp_query?

This is what i ran. But no luck.

$my_wp_query = new WP_Query();
          $all_wp_pages = $my_wp_query->query(
                  array(
                        'post_type' => 'page',
                        //'post_parent' => 'keepers-gallery'
                      ));
            $events = get_page_by_title('Events');
            $events_children = get_page_children( $events->ID, $all_wp_pages );

             //print_r( $events_children, true );

            var_dump($events_children, true);

Thats very strange. I tried the code myself, and I get the children displayed. You have double checked that the page you want is set to have Events to be parent in the admin area?

Punal Chotrani
Punal Chotrani
8,817 Points

The problem is that i have two pages called 'Events', and it's picking up the other one.

'Events' is a child of the page 'Gallery' which has been already set in the admin area. There is also page 'Events' which is a sibling of 'Gallery'.

How do you target the child_page in 'Events' which has the parent 'Gallery'?

exactly the same as the code I tried above. But in your case, you have to use this code :

$portfolio = get_page_by_title('Gallery');

The rest of the code is the same as before.