query_post返回所有帖子,即使我为分类法指定了特定值

时间:2014-08-14 17:47:14

标签: wordpress custom-post-type

我有一个简单的query_posts使用自定义帖子类型和链接到它的分类。我使用插件MAGIC FIELD来实现我的目标。

这是代码。

<?php
    // Display the persons with Sport Injuries Speciality //
      wp_reset_query();

      // Args
      $args = array(
          'post_type' => 'physician',
          'order' => 'ASC',
          'orderby' => 'menu_order',
          'type_of_speciality' => 'Sport Medecine specialist'
          );

      // The Query
      query_posts( $args );

      // The Loop
      while ( have_posts() ) : the_post();
      result here
      endwhile;
      wp_reset_query();
 ?>

正如您所看到的,我想只显示“体育医学专家”的帖子。因为有特色。管理员必须检查单选按钮。

我有什么遗漏,因为我应该只有3个结果,它会为我提供此自定义帖子类型的所有帖子。

编辑#1:

我做了一些工作,但这对速度优化并不是很好,因为它在所有自定义帖子类型中循环。

 <?php
    // Display the persons with Sport Injuries Speciality //
      wp_reset_query();

      // Args
      $args = array(
          'post_type' => 'physician',
          'order' => 'ASC',
          'orderby' => 'menu_order',
          );

      // The Query
      query_posts( $args );

      // The Loop
      while ( have_posts() ) : the_post();
      if(get('type_of_speciality') == 'Sport Medecine specialist')
      { 
         echo the_title();
      }
      endwhile;
      wp_reset_query();
?>

如您所见,if()条件检查&#34;分类法&#34;如果值不匹配,则将Sport Medecine专家作为值,exit(else)

就像我说的那样,这并不是很好,因为如果我有1000名医生,它就会循环使用1000。

有什么想法照亮我吗?

1 个答案:

答案 0 :(得分:1)

更改查询方式,而不是税收=&gt;值,使用tax_query,而不是使用query_posts()使用WP_Query

看看它是否适合你。

WP_Query taxonomy parameters

<?php 

$args = array(
    'post_type' => 'physician',
    'order'     => 'ASC',
    'orderby'   => 'menu_order',
    'tax_query' => array(
        array(
            'taxonomy' => 'type_of_speciality',
            'field'    => 'slug',   //term_id or slug
            'terms'    => 'sport-medecine-specialist',  // the term ( id or slug ) based on the value of field above
        ),
    ),
);

$search = new WP_Query( $args );

while ( $search->have_posts() ) : $search->the_post();

    the_title();

endwhile;
wp_reset_postdata();