[英]List custom post types with custom taxonomy category assigned to them
I have a custom post type called products
and custom taxonomy called product-category
.我有一个名为
products
的自定义帖子类型和一个名为product-category
的自定义分类。 Now I need to list all the posts from this product post type which have product-category assigned to them.现在我需要列出此产品帖子类型中分配了产品类别的所有帖子。 I thought something like this could work, but no luck.
我认为这样的事情可以工作,但没有运气。 Could anyone point me in the right direction, please?!
谁能指出我正确的方向,好吗?! Really appreciate your help!
非常感谢您的帮助!
<?php $category = get_search_query();
$args = array(
'taxonomy' => 'product_category',
'post_type' => 'products',
'category_name' => $category
);
$query = new WP_Query( $args );
// OR
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-category',
'field' => 'slug',
'terms' => $category
)
)
);
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ): ?>
<div class="products">
<ul>
<?php while ( $query->have_posts() ): $query->the_post(); ?>
<li><?php get_template_part( 'template-parts/content', 'search' ); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php else: ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You might need to add the relation
to your args.您可能需要将
relation
添加到您的参数。
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_category',
'terms' => $category,
'field' => 'slug',
),
),
);
Also, check:另外,检查:
products
and not product
?)products
而不是product
吗?)product_category
but in your second query you have product-category
.product_category
,但在第二个查询中,您有product-category
。 Should it be a hyphen or an underscore?$category
valid? $category
返回的值是否有效?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.