2016-05-23 14 views
0

我試圖得到category和我的自定義分類Type的帖子的結果。我想這WordPress的query_posts爲多個變量不工作

$args = 'category=' . $cat . '&Type='.$type.'&order=ASC'; 
    query_posts($args); 

我從一個GET請求接收$cat$type。問題是這是拉起屬於Type分類標準的所有帖子到category

您的幫助表示讚賞。

謝謝!

+0

請勿使用'query_posts()'獲取帖子。對於一般的後期查詢,請使用['WP_Query()'](https://codex.wordpress.org/Class_Reference/WP_Query)或['get_posts()'](https://codex.wordpress.org/Function_Reference/get_posts )。 –

回答

1

我不得不這樣做:

$args = array(
    'post_type' => 'post', 
    'tax_query' => array(
     'relation' => 'AND', 
     array(
      'taxonomy' => 'category', 
      'field' => 'slug', 
      'terms' => array($cat), 
     ), 
     array(
      'taxonomy' => 'Type', 
      'field' => 'slug', 
      'terms' => array($type), 
     ), 
    ), 
); 
$query = new WP_Query($args); 

而是採用query_posts()

謝謝!

+0

僅供參考...不要使用'query_posts',爲什麼? [點擊這裏](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts) – Noman

+0

謝謝你這麼多分享! –