在 WordPress 的类别中显示具有 And 关系的帖子
show posts with And relation in categories at WordPress
我想在查询中显示类别(a、b 和 c)与 And Relation 的帖子。
我写了下面的代码,但它显示了每个类别的所有帖子。如何显示所有 3 个类别都具有 And 关系的帖子?
<?php
$args2 = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 6,
'relation' => 'And',
'category_name'=>'a','b','c',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
$q = new WP_Query($args2);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
echo the_title();
}
}
?>
我添加了税务查询,现在它会检查 post 是否有类别 a、b 或 b 的父级以及位置集。
$args2 = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'tax_query' => array(
// If 'a' is set.
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'a' ),
'operator' => 'IN'
),
// AND if 'b' OR 'parent_b' is set.
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'b', 'parent_b' ),
'operator' => 'IN'
),
// AND if location is set.
array(
'taxonomy' => 'location',
'operator' => 'EXISTS'
),
)
);
$q = new WP_Query( $args2 );
if( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
echo the_title();
}
}
我想在查询中显示类别(a、b 和 c)与 And Relation 的帖子。 我写了下面的代码,但它显示了每个类别的所有帖子。如何显示所有 3 个类别都具有 And 关系的帖子?
<?php
$args2 = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 6,
'relation' => 'And',
'category_name'=>'a','b','c',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
$q = new WP_Query($args2);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
echo the_title();
}
}
?>
我添加了税务查询,现在它会检查 post 是否有类别 a、b 或 b 的父级以及位置集。
$args2 = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'tax_query' => array(
// If 'a' is set.
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'a' ),
'operator' => 'IN'
),
// AND if 'b' OR 'parent_b' is set.
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'b', 'parent_b' ),
'operator' => 'IN'
),
// AND if location is set.
array(
'taxonomy' => 'location',
'operator' => 'EXISTS'
),
)
);
$q = new WP_Query( $args2 );
if( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
echo the_title();
}
}