添加自定义帖子以在Wordpress中搜索(单独到类别)


Add custom posts to search (separately to categories) in Wordpress

我有一个正在建设的搜索结果页面,它将帖子分成类别(产品、食谱、文章),在页面下方的 3 个独立部分中。类别(食谱和文章)很好且简单 - 创建一个查询以在 2 个单独的循环中使用,但我在自定义帖子类型方面遇到了问题。

如果可能的话,我也想将它们与循环一起拉入,但不确定是在帖子类型还是分类法上执行此操作。这些产品显然在分类法下分为子类别,所以当我尝试时,它多次拉动相同的产品。

现在,在放弃尝试使用分类法将它们拉过之后,我正在尝试这样做:

<?php
    $args = array(
        'post_type'=> 'products'
    );
    $products = new WP_Query($args);
    if ( have_posts() ) :
?>
        <div class="product suggestions cfx sub-range">
            <h2>We found xx products...</h2>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>

这段代码没有通过任何内容...其他查询是否有效,还是仍应尝试使用自定义分类?

当我搜索解决方案时,我能找到的几乎所有内容都是创建一个搜索.php页面,仅显示自定义帖子类型,或者只是将自定义帖子类型添加到搜索结果中,而不是自定义帖子类型的单独循环以及类别循环......

谢谢

我想

我明白了。我从产品中更改了子弹,由于某种原因它开始工作。所以现在我有这个:

<?php
    $args = array ('post_type'=>'product-ranges');
    $products = new WP_Query($args);
    if ( have_posts() ) :
?>
        <div class="product suggestions cfx sub-range">
            <h2>We found xx products...</h2>
<?php while ( $products->have_posts() ) : $products->the_post();
 ?>