Return all page_names in WordPress, without excess?
I need to retrieve an array of every page_name registered in WordPress. I've got two issues, I can do a get_pages() and such, but it literally pulls every freakin thing about each page including their content. Totally unnecessary overhead when all I need is page_name for each.
The other is that I'd like to do it with a built in method if possible, as this is for an in-house plugin and we'd like to keep it compatible with the mainline. (worst case will just access the DB directly and get them) I know you can include/exclude in the get_pages() call, but I haven't figured out if it is possible to exclude retrieving everything but one, instead of the opposite.
It needs to be dynamic in that it cannot have any hard-coded strings i.e. know anything about the pages themselves or what they're called. Also no extra junk like it being in an unordered list or something. Straight up array, no levels needed. (sub-pages are listed same as primary)
Any ideas guys? I've searched and searched..but the documentation is retarded for these types of things as you guys probably know.
Thanks.
Example of what I'd like in the end or similar:
Array
(
[0] => stdClass Object
(
[page_name] => 'page1'
)
[1] => stdClas开发者_运维技巧s Object
(
[page_name] => 'page2'
)
[2] => stdClass Object
(
[page_name] => 'page3'
)
[3] => stdClass Object
(
[page_name] => 'page4'
)
)
To limit the fields returned, you can set up a filter. In your themes functions.php file, or a plugin, try
add_filter( 'get_pages', 'get_pages_filter' );
function get_pages_filter( $res ){
$res = array_map( 'get_pages_title', $res );
return $res;
}
function get_pages_title( $item ){
return (object) array( 'page_name' => $item->post_name );
}
$pages = get_pages();
var_dump( $pages );
I checked get_pages()
source code and there's no way you can limit what's being requested from a database. Pages are fetched at lines 3177-3184, and as you can see, there's a hardcoded SELECT * FROM
query.
If anyone has a clean solution, please chime in. For now I'm just going to use WordPress's built in DB connection and grab them manually.
For the curious...I just threw this together quick...probably not the best..
/**
* get_wpdb_values()
*
* DESC:
*
* Allows you to make a WP Database connection
* and return an Array => Object of what ever
* values you need from a table.
*
* Was made for the purpose of returning a page
* list, but since you pass it the field you
* want returned, along with with optional filters
* it can easily be used for other purposes.
*
* USAGE:
*
* array get_wpdb_values ( string $table [, string $field [, array $filters ]] )
*
* PARAMETERS:
*
* ----------|-----------------------------------------------------------------
* $table | Required table you want to return values from.
* | DO NOT INCLUDE THE WP PREFIX! (e.g. use 'posts' not 'wp_posts'
* ----------|-----------------------------------------------------------------
* $field | Optional field name you want returned. (Default returns * all)
* ----------|-----------------------------------------------------------------
* $filters | Optional filtering passed as field => value array.
* ----------|-----------------------------------------------------------------
*/
function get_wpdb_values( $table = null,
$field = "*",
$filters = null )
{
// Get access to the
// WordPress Database
// class in this scope
global $wpdb;
// If we weren't passed any
// arguments, get out quick
if(is_null($table) || empty($table))
return false;
// Add optional filters if
// they were passed in
if(!is_null($filters))
{
// Counter is so we can tell
// if there is more then one
// filter so we can add the
// AND separator in the
// SQL query
$WHERE = "WHERE ";
$counter = 0;
foreach ($filters as $key => &$value)
{
$counter++;
// If we're on the second or more
// pair, we add the AND to chain
// conditional WHERE's
$AND = ($counter >= 2) ? " AND" : null;
// Append to existing WHERE
// statement
$WHERE .= "$AND $key = '$value' ";
}
}
else
{
// No filters passed
$WHERE = null;
}
// Get WordPress formatted
// table name
$wp_table = $wpdb->$table;
// Putting it all together
$query = "SELECT $field FROM $wp_table $WHERE ";
// Make actual DB call
// through the built in
// MySQL interface for
// WordPress to at least
// attempt to remain
// compatible with mainline
return $wpdb->get_results($query);
}
精彩评论