2016-12-01 34 views
0

我想抓住所有包含meta key'basePrice'的Wordpress页面,而不管元值如何。如何获取包含相同元键的所有页面?

当我尝试做一个简单的get_pages()时,返回一个空数组。 根据WordPress的文档,它指出,meta_value要求meta_key工作,但不是相反,所以它应该工作?

$basePrices = get_pages(array(
    'meta_key' => 'basePrice' 
)); 

如何获得所有在我的数组中有一个名为'basePrice'的元键的页面?

回答

0

首先,您应该为这些复杂查询使用WordPress查询对象。这会给你更多的参数。

所以,你可以这样做:

// Let's prepare our query: 
$args = array(
    'post_type' => 'page', 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'basePrice', 
      'compare' => 'EXISTS' 
     ), 
    ) 
); 
$the_query = new WP_Query($args); 

// Array to save our matchs: 
$pages = array(); 

// The Loop 
if ($the_query->have_posts()) { 

    while ($the_query->have_posts()) { 

     // Let's take what we need, here the whole object but you can pick only what you need: 
     $pages[] = $the_query->the_post(); 

    } 

    // Reset our postdata: 
    wp_reset_postdata(); 
} 

这应该只是罚款。

使用get_pages()的另一种方式是获取所有页面 - >循环它们 - >创建一个get_post_meta()if语句。如果有值,则将当前页面添加到您的阵列。但是,正如你可以想象的,你必须加载所有页面,而你不应该。

希望有帮助,

相关问题