2013-05-30 26 views
0

我有一個基本的WordPress循環,它工作正常,並輸出帖子,直到我嘗試向循環添加分類參數。一旦我將其添加到循環中,它就會停止輸出任何內容,但不會給出任何PHP錯誤。循環如下。通過分類不能正常工作的WordPress查詢

<?php 
    $newsLoop = new WP_Query(array('post_type' => 'news&events', 'taxonomy' => 'postcategory', 'term' => 'featured', 'posts_per_page' => 3, 'orderby' => 'post_date', 'order' => 'ASC')); 
    while ($newsLoop->have_posts()) : $newsLoop->the_post(); 
?> 

<div class="newsEvent"> 

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

<?php the_excerpt(); ?> 

<p style="text-align:center;"><a href="<?php the_permalink(); ?>">Read More</a></p> 

</div> 

<?php endwhile; wp_reset_postdata(); ?> 

我試過一起刪除帖子類型參數,只是查詢分類,沒有運氣那裏。有什麼我在這裏失蹤或不正確?

回答

0

您必須使用tax_query參數:

$args = array(
    'post_type' => 'news&events', 
    'tax_query' => array(
     array(
      'taxonomy' => 'postcategory', 
      'field' => 'slug', 
      'terms' => 'featured' 
     ) 
    ), 
    'posts_per_page' => 3, 
    'orderby' => 'post_date', 
    'order' => 'ASC' 
); 
$newsLoop = new WP_Query($args); 

檢查文檔:http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

+0

我其實也試過這個。同樣的結果。而且,在通過分類法查詢時,這不是必需的。對於更復雜的分類標準查詢是可選的。 – AndyWarren

+0

你有沒有仔細檢查你的值?你可能打算查詢'post_type =>數組('news','events')'? – diggy

+0

我已多次檢查所有值。帖子類型是調用新聞和事件,以便查詢是正確的。這是非常奇怪的行爲,查詢很有效,直到我引入任何分類學參數。 – AndyWarren

1

我有類似的東西幾個星期前,這裏曾是我對你:

在Functions.php中:(註冊分類)。

$args = array(
     'hierarchical'  => true, 
     'labels'   => $labels, 
     'show_ui'   => true, 
     'show_admin_column' => true, 
     'query_var'   => true, 
     'rewrite'   => array('slug' => 'news-category'), 
    ); 
register_taxonomy('news-tax', array('news&events'), $args); 

現在查詢:

<?php $args = array('news-tax' => 'postcategory', 'post_type' => 'news&events', 'orderby' => 'post_date','order' => 'ASC', 'posts_per_page' => -1); 
        $loop = new WP_Query($args); 
        while ($loop->have_posts()) : $loop->the_post();?> 

讓我知道這是否正常工作!另外,如果沒有任何分類法,您是否能夠成功查詢「新聞&事件」?也許&安培是造成問題?無論哪種方式,我嘗試了很多方法,上面的工作對我來說是無縫的。