List pages in wordpress into a PHP Array
I spent some time figuring out how to get the wordpress wp_list_pages function to return the results as a PHP array of URLs and page names. There wasn’t an obvious way to do this and searching google did not yield anything particularly useful. The wp_list_pages function has an “echo” parameter which when set to 0 returns a list of links as a string. That won’t help much if you want it as an array with links and page names.
I ended up writing a short script that will query wordpress for the name and URL of all of the subpages of a specified page. This could be used to make a function that formatted a list of pages a particular way (like making the list wrap into two or more columns). Its nothing fancy, but perhaps it can help other wordpressers who are looking for something similar.
<?php
$querystr = “SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘page’ AND $wpdb->posts.post_parent = [[Parent Page ID]] ORDER BY $wpdb->posts.post_title ASC”;$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
// Add your own logic or formatting here
?>
<a href=”<?php the_permalink() ?>” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a>
<?php endforeach;
else :
echo ‘Not Found’;
endif;
?>
Where [[Parent Page ID]] is the id number of the parent page you would like to get children from. If you want all pages included, don’t include this parameter.





Pangeran http://pangeran.org/
July 27th, 2008 at 1:40 pmThanks for this code.
But how to remove the [[Parent Page ID]] .
I want all pages is listed.
I just want to add [b] before the [a]
Brendan Nee http://www.bnee.com
July 27th, 2008 at 9:34 pmIf you wanted to return a list including all published pages, just remove the section:
AND $wpdb->posts.post_parent = [[Parent Page ID]]
entirely.
Pangeran http://pangeran.org/
July 28th, 2008 at 2:19 amI’ve tried that(Removed AND $wpdb->posts.post_parent = [[Parent Page ID]] )
And I’ve tried to put page id inside like this [[1 2 2 34]]
Both not working…
I am using WordPress 2.6
And using OPTION theme by Justin Tadlock to intergrate this…
Brendan Nee http://www.bnee.com
July 28th, 2008 at 2:38 amI haven’t checked this with wordpress version 2.6 yet, let me know if you figure this out.
Pangeran http://pangeran.org/
July 28th, 2008 at 3:05 amOh?
Thanks anyway…
I just use non dynamic html for now…
Thanxer
July 31st, 2008 at 12:49 pmThanks bud, You saved my Time.
I was Looking in wp_list_pages without result!
Dyasonhat http://www.dyasonhat.com
September 11th, 2008 at 8:48 pmThanks, working great!
Will Anderson http://www.itsananderson.com
September 18th, 2008 at 5:21 pmHi,
I too feel that the wp_list_pages function leaves something to be desired. I went about fixing it in another way though. the wp_list_pages function uses another function called get_pages which returns a list of page IDs. Using that, you can retrieve the post name and it’s URL. The place I found this useful was in an implementation of an expanding page list for a client. doing it with wp_list_pages would have been next to impossible, but using the get_pages function, it was almost trivial.
The reason I suggest using this over a database call is that the database call can easily be broken in a later release if the database schema changes. On the other hand, as long as the get_pages function remains consistent, a later version won’t break the code.
Anyway, those are just my ideas on maintainability. Using database calls will work, I just wouldn’t suggest it when there’s an alternative.
Will