2016-03-01 155 views
1

這是我運行的顯示2個類別帖子的自定義查詢。 我已經安裝了WordPress Plugin「精選文章」,但精選文章並未排除在顯示列表中。排除自定義查詢中的精選帖子WordPress

<?php 
    $category_id = get_cat_ID($strReports || $strInsights); 
    $custom_query = new WP_Query('cat=' .$category_id. '&featured=no&posts_per_page=6&order=desc'); 
    while($custom_query->have_posts()) : $custom_query->the_post(); 
?> 

HTML Content Here 

<?php endwhile; ?> 
<?php wp_reset_query(); // reset the query ?> 
+0

確定功能=否是正確的查詢參數嗎? – Blackbam

回答

2

通過尋找「特色郵報」你可以看到,它只是測試對值。什麼feature=yes不只是檢查薈萃現場,所以我覺得你可以做反向的方式來實現你想要什麼,就像這樣:

$args = array(
    'cat' => $category_id, 
    'posts_per_page' => 6, 
    'order' => 'DESC', // since order default value is already DESC you can remove this line 
    'meta_query' => array(
     'relation' => 'OR', 
     array(
      'key' => '_is_featured', 
      'value' => 'yes', 
      'compare' => '!=', 
     ), 
     array(
      'key' => '_is_featured', 
      'value' => 'foo', // Prior to WP 3.9 we have to provide any non-empty string here 
      'compare' => 'NOT EXISTS', 
     ), 
    ) , 
); 
$custom_query = new WP_Query($args); 

希望它能幫助!

+0

不工作。它只顯示單個帖子 –

+0

爲我工作,非常感謝 – Adrian

1

your'e在get_cat_ID上運行無效的參數。

應該是這樣的:

<?php 
    $cat_names = array('Cat_Name1', 'Cat_Name2'); 
    $category_Name = implode(',', $cat_names); 
    $args = array(
     'category_name' => $category_Name, 
     'posts_per_page' => 6, 
     'meta_query' => array(
       array(
        'key' => 'featured', 
        'value' => true, 
        'compare' => '!=', 
       ), 
      ), 
    ); 
    $custom_query = new WP_Query($args); 
    while($custom_query->have_posts()) : $custom_query->the_post(); 
?> 

<?php the_title();?> 

<?php endwhile; ?> 
<?php wp_reset_query(); // reset the query ?> 
+0

我已經添加了相同的代碼,你已經給出,但現在沒有帖子顯示。 –

0

創建自定義查詢,並添加過濾器。

function SearchFilter($query){ 

$query=" SELECT Distinct SQL_CALC_FOUND_ROWS p.* FROM `$wpdb->posts` as p left join `$wpdb->postmeta` as m on m.post_id=p.ID " 
       ." left join `$wpdb->term_relationships` as r ON (p.ID = r.object_id) " 
       ." left join `$wpdb->term_taxonomy` as x ON (x.term_taxonomy_id = r.term_taxonomy_id) " 
       ." left join `$wpdb->terms` as t ON (t.term_id = x.term_id AND x.taxonomy='category') " 
       ." WHERE p.post_type = 'post' AND p.post_status = 'publish' " 
       ." AND t.term_id='$idcategorie' " 

return $query; 
} 
add_filter('posts_request', 'SearchFilter'); 
相關問題