admin管理员组

文章数量:1323150

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.

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.

Share Improve this question asked Apr 21, 2011 at 18:26 Scott BScott B 5,69614 gold badges94 silver badges148 bronze badges 6
  • 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/Function_Reference/wpdb_Class – Cristian Commented Apr 21, 2011 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 Commented Apr 21, 2011 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 Commented Apr 21, 2011 at 19:07
  • @Bainternet: currently I'm just using $thePages = get_pages('post_type=page&post_status=publish&parent=0'); – Scott B Commented Apr 21, 2011 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 Commented Sep 2, 2020 at 0:55
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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
    }
}

本文标签: plugin developmentHow to exclude content (and other returned values) from WPquery()