2016-04-15 58 views
0

我有這個循環,我需要顯示具有某個meta_value的帖子的所有標題,或者沒有meta_key' the_status'。顯示具有某個meta_key值或不具有meta_key的帖子

我的帖子目前使用名爲 'the_status' 的meta_key並且可以有這些meta_key值:

幫助 not_helping finished_helping

...或一個職位可能甚至沒有meta_key「the_status '一點都不。

這是我有:

<?php 
$the_query = array(
    'posts_per_page' => -1, 
    'author'   => $current_user->ID, 
    'post_status'  => 'publish', 
    'meta_key'   => 'the_status', 
    'meta_value'  => array('helping') 
); 
$help_posts = new WP_Query($the_query); 
while($help_posts->have_posts()) : $help_posts->the_post(); 
?> 

<p><?php the_title(); ?></p> 

<?php 
endwhile; 
?> 

這顯然只給我說有一個meta_value「幫助」的帖子的標題,但它也需要表明沒有meta_key的帖子的標題the_status'。

感謝您的閱讀。

回答

1

更換

'meta_key'   => 'the_status', 
'meta_value'  => array('helping') 

隨着

'meta_query' => array(
    'relation' => 'OR', 
    array(
     'key' => 'the_status', 
     'compare' => 'NOT EXISTS', 
     'value' => '' //can be omitted in WP 3.9+ 
    ), 
    array(
     'key' => 'the_status', 
     'value' => array('helping') 
    ) 
+0

感謝。這有效,並回答我的問題。但是我知道一些meta_values是NULL(我注意到我在PHPMyAdmin中)。 我該如何修改它以包含NULL meta_values? – user3256143

+0

我沒有可以玩的WP安裝來測試,但我會嘗試添加另一個陣列:'array( 'key'=>'the_status', 'value'=> NULL )' – trex005

+0

沒有工作(儘管我同意它應該有)。我願意接受其他建議。儘管如此,我已將您的原始答案標記爲正確。謝謝! – user3256143

相關問題