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
1 Answer
Reset to default 1Like 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()
版权声明:本文标题:plugin development - How to exclude content (and other returned values) from WP_query()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742143760a2422697.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论