Does a method exist that can return a simple array of the pages in the site that are published? I only want the name and the slug returned, not the full content or other default values.

I've tried WP_query(), get_posts(), get_pages() and query_posts() but they all return the post/page content. I'm only looking to get the post name and slug returned.

If there's no lighter method, I can use one of these. Just want to make sure.

Any help, much appreciated.

  • I don't think there is, the only way to do that I think would be a custom query using the WPDB object. codex.wordpress.org/Function_Reference/wpdb_Class – Cristian Apr 21 '11 at 18:37
  • Thanks. I wonder which of all of the above is the least resource intensive? I only need to grab the post_name and slug for about 5-6 pages so that I can add them to a custom menu on the fly. – Scott B Apr 21 '11 at 18:54
  • how are you selecting these few pages? whats the query args? I'm asking because i know there is no way other the a custom sql query which is not that hard to do. – Bainternet Apr 21 '11 at 19:07
  • @Bainternet: currently I'm just using $thePages = get_pages('post_type=page&post_status=publish&parent=0'); – Scott B Apr 21 '11 at 19:22
  • This gives me a hint. Has anything changed in Wordpress since the last post? Unfortunately I have not found anything on Google about how to set the return values in WP_Query. Is there a best practice available? It is very inefficient to get stuff by e.g. SELECT * FROM wp_posts WHERE ID = 1141 LIMIT 1 when you only need two fields from the post. – Steffan Schlüter Sep 2 at 0:55

Like a commented the only way to do it is with a custom sql query so:

global $wpdb;
$mypages = $wpdb->get_results( "SELECT post_title, post_name FROM $wpdb->posts
 WHERE post_type = 'page' 
 AND post_status = 'publish' 
 AND parent = '0'");

if (count($mypages) > 0){
    foreach ($mypages as $page){
        //do you stuff
        //$page['post_title'] for title
        //$page['post_name'] for slug
| improve this answer | |
  • Thanks Bainternet. I'm assuming this is much lighter than my get_pages call. – Scott B Apr 21 '11 at 19:49

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.