Wordpress查询类别

时间:2014-02-11 08:36:00

标签: php wordpress post categories

我面临一个奇怪的问题。我想只显示第7类中的帖子。但是使用以下代码它并没有向我显示任何内容:

$featured = new WP_Query(array('post_type' => 'tours', 'posts_per_page' => 3, 'order' => 'DESC', 'orderby' => 'id', 'cat' => 7));
                if($featured->have_posts()) {
                    while($featured->have_posts()) : $featured->the_post();
                        echo the_title();
                    endwhile;
                }

我也在用这个:

$featured = new WP_Query(array('post_type' => 'tours', 'posts_per_page' => 3, 'order' => 'DESC', 'orderby' => 'id', 'category__in' => 7));

但没有任何反应。

4 个答案:

答案 0 :(得分:0)

致电$featured = new WP_Query(array('post_type' => 'tours', 'posts_per_page' => 3, 'order' => 'DESC', 'orderby' => 'id', 'cat' => 7));后立即

致电echo $featured->found_posts并查看您的查询中是否有帖子

答案 1 :(得分:0)

请试试这个

 $args=array(
                      'category__in' => array('7'),
                      'post_type' => 'tours',
                      'post_status' => 'publish',
                      'posts_per_page' => -1,
                      'caller_get_posts'=> 1
                    );
 $featured = new WP_Query($args);
 if($featured->have_posts()) {
                while($featured->have_posts()) : $featured->the_post();
                    echo the_title();
                endwhile;
            }

答案 2 :(得分:0)

试试这个

    $featured = new WP_Query(array( 'post_type' => 'tours', 'cat' => 7, 'order' => 'DESC', 'orderby' => 'id', 'posts_per_page' => 10 ));
    if($featured->have_posts()) {
      while ($featured->have_posts()) : $featured->the_post();
        the_title();
      endwhile;
    }

答案 3 :(得分:0)

嗨Nadeem试试这个我希望你能找到答案

   $featured = new WP_Query(
    array(
        'post_type' => 'tours',
        'posts_per_page' => 1,
        'order' => 'DESC',
        'orderby' => 'id',
        'tax_query' => array(
                   array(
                    'taxonomy' => 'tours_cat',
                    'field' => 'id',
                    'terms' => array(7),
                    ),
                ),
            )
        );