Brendan Nee

List pages in wordpress into a PHP Array

April 10th, 2008

Update: Thanks to Joost in the comments for suggesting the use of get_pages() to accomplish the same thing as what I posted below. He posted the example:

This seems much better than what I had previously used, but for reference, my old post is below:

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.

  • Thanks 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]

  • If you wanted to return a list including all published pages, just remove the section:

    AND $wpdb->posts.post_parent = [[Parent Page ID]]

    entirely.

  • I’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…

  • I haven’t checked this with wordpress version 2.6 yet, let me know if you figure this out.

  • Oh?
    Thanks anyway…
    I just use non dynamic html for now…

  • Thanxer

    Thanks bud, You saved my Time.
    I was Looking in wp_list_pages without result!

  • Thanks, working great!

  • Hi,
    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

  • Isn’t this what get_pages() is for?

    $pages = get_pages(‘child_of’ => [[THE PARENT ID]], ‘sort_order’ => ‘ASC’);
    foreach ($pages as $page) {
    // Do your thang
    }