Wordpress query\u post返回所有post,即使我为分类法指定了特定值
我有一个简单的Wordpress query\u post返回所有post,即使我为分类法指定了特定值,wordpress,custom-post-type,Wordpress,Custom Post Type,我有一个简单的query\u posts,使用自定义post类型和链接到它的分类法。我使用插件来实现我的目标 这是代码 <?php // Display the persons with Sport Injuries Speciality // wp_reset_query(); // Args $args = array( 'post_type' => 'physician', 'order' =
query\u posts
,使用自定义post类型和链接到它的分类法。我使用插件来实现我的目标
这是代码
<?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()
条件检查“分类法”是否将Sport Medecine specialist作为值,如果值不匹配,则检查exit(else)
正如我所说的,这不是很好,因为如果我有1000名医生,它会使1000名医生遭受重创
有什么想法可以帮我点灯吗?改变你查询它的方式,而不是tax=>value,使用tax\u query,而不是使用
query\u posts()
useWP\u query
看看它是否适合你
type\u of\u speciality是一种分类法,对吗?不要使用query\u posts
。就像在永不使用it@PieterGoosen你能链接一个页面,让他们解释为什么我不应该再使用它吗?阅读法典,这也是我的重点。我认为这个解决方案可以奏效。但我需要知道这一点。Terms=返回的值?我只是试着复制粘贴,但没有显示任何内容。我们需要知道我的专业类型是用magic field插件制作的。术语
等于术语的slug,在那个例子中,我不知道slug是什么,您应该在其中放入正确的slug,以便代码工作,或者将字段的值更改为id,然后在术语中放入分类id,然后它应该工作。
<?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();