我需要检索在WordPress中注册的每个page_name的数组。我有两个问题,我可以做一个get_pages()等等,但它实际上拉动了每个页面的每一个奇怪的事情,包括它们的内容。当我需要的是每个的page_name时,完全不必要的开销。
另一个是我想尽可能使用内置方法,因为这是内部插件,我们希望它与主线保持兼容。 (最糟糕的情况是直接访问数据库并获取它们)我知道你可以在get_pages()调用中包含/排除,但我还没有想出是否可以排除检索除了一个之外的所有内容,而不是相反。
它需要是动态的,因为它不能有任何硬编码的字符串,即了解页面本身或它们被称为什么。也没有额外的垃圾,就像它在无序列表或其他东西。直线阵列,不需要任何级别。 (子页面与主要页面相同)
任何想法的家伙?我已经搜索过并搜索过了。但是你们可能知道这些类型的东西的文档是迟钝的。
感谢。
我最终或类似的例子:
Array
(
[0] => stdClass Object
(
[page_name] => 'page1'
)
[1] => stdClass Object
(
[page_name] => 'page2'
)
[2] => stdClass Object
(
[page_name] => 'page3'
)
[3] => stdClass Object
(
[page_name] => 'page4'
)
)
答案 0 :(得分:2)
要限制返回的字段,您可以设置过滤器。在你的主题functions.php文件或插件中,试试
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 );
答案 1 :(得分:0)
我检查了get_pages()
source code,并且您无法限制从数据库中请求的内容。页面是在lines 3177-3184获取的,如您所见,有一个硬编码的SELECT * FROM
查询。
答案 2 :(得分:0)
如果有人有一个干净的解决方案,请加入。现在我将使用WordPress的内置数据库连接并手动抓取它们。
对于好奇......我只是快速地把它扔到一起......可能不是最好的......
/**
* 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);
}