简体   繁体   English

列出具有分配给它们的自定义分类类别的自定义帖子类型

[英]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:另外,检查:

  • Is the post type spelled correctly (ie Are you sure it's products and not product ?)帖子类型拼写是否正确(即您确定它是products而不是product吗?)
  • In your first query you have product_category but in your second query you have product-category .在您的第一个查询中,您有product_category ,但在第二个查询中,您有product-category Should it be a hyphen or an underscore?它应该是连字符还是下划线?
  • Is the value returned by $category valid? $category返回的值是否有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有自定义类别/分类的分页自定义帖子类型 - Pagination Custom Post Type with Custom Category/Taxonomy 为自定义帖子类型类别列表添加类“ current_page_item”(注意:该类别属于自定义分类法) - Adding class “current_page_item” for custom post type category list(note: the category is belong in custom taxonomy) WordPress,将帖子添加到自定义分类的类别 - Wordpress, add post to custom taxonomy's category 自定义分类类别列表中的“所有按钮” - All Button in a custom taxonomy category list 类别页面上的自定义帖子类型的自定义分类法 - Custom taxonomy thumnbails for custom post type on category page 具有自定义帖子类型和类别ID的自定义查询 - Custom Query with custom post types and category id 带有自定义帖子类型和自定义分类标签的 ACF 前端表单 - ACF frontend form with custom post types and Custom taxonomy tags 在 wordpress 自定义帖子中按分类获取页面列表 - Getting pages list by taxonomy in wordpress custom post 从自定义帖子类型中删除默认的“类别”分类法 - Remove default “category” taxonomy from custom post type WordPress:使用post__not_in排除自定义分类类别 - Wordpress: Using post__not_in to exclude custom taxonomy category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM