wp_query with cat not working

时间:2015-08-03 作者:Matthieu Boisjoli

我试图从特定的父类别中检索一些自定义帖子类型。

 <div class="tab-content">
<?php 
  $cat_menu = get_categories(\'taxonomy=menu_categorie&type=menus\');
  foreach($cat_menu as $menu) {
    echo \'<div role="tabpanel" class="tab-pane active fade" id="\'.$menu->slug.\'">\';
    $id = (int) $menu->cat_ID;
    $args = array(
          \'post_type\' => \'menus\',
          \'post_status\' => \'publish\',
          \'cat\' => $id
        );
    $recipes = new WP_Query($args);
    if($recipes->have_posts()) : while($recipes->have_posts()) : $recipes->the_post();
      echo the_title();
    endwhile; endif;
    echo \'</div>\';
  }
?>
</div>
如果我在数组$args中删除键“cat”,它会工作,但会输出我的所有菜单。我想要的是能够从category\\u id输出我的菜单帖子类型,但我没有显示任何内容。所以我猜$配方中什么都没有,然后就失败了。

在检索具有特定类别id的自定义帖子类型时,我是否遗漏了什么?

2 个回复
SO网友:terminator

从你目前所写的来看menu_categorie 是自定义分类法。确保仅限于此。我有种感觉,你错过了menu_categorie

无论如何

论点\'cat\' => $id 您使用的用于默认分类法,即category.

但在你的情况下,你有自定义的分类法menu_categorie.

因此,您需要使用tax\\u查询。

这就是您的代码的外观。

<div class="tab-content">
<?php 
  $cat_menu = get_categories(\'taxonomy=menu_categorie&type=menus\');
  foreach($cat_menu as $menu) {
    echo \'<div role="tabpanel" class="tab-pane active fade" id="\'.$menu->slug.\'">\';
    $id = (int) $menu->cat_ID;
    $args = array(
          \'post_type\' => \'menus\',
          \'post_status\' => \'publish\',
          \'tax_query\' =>
                      array(
                        array(
                          \'taxonomy\' => \'menu_categorie\',
                          \'field\'    => \'id\',
                          \'terms\'    => $id
                        ),
                      ), 
        );
    $recipes = new WP_Query($args);
    if($recipes->have_posts()) : while($recipes->have_posts()) : $recipes->the_post();
      echo the_title();
    endwhile; endif;
    echo \'</div>\';
  }
?>
</div>
这应该行得通。如果不行,请告诉我

SO网友:Андрей Гаврилов

use

  <?php $category = get_queried_object();
                                $categoryOne =  $category->term_id;
                                $args = array(
                                    \'post_type\' => \'iy-portfolio\',
                                    \'posts_per_page\' => 6,
                                    \'tax_query\' => array(
                                        array(
                                            \'taxonomy\' => \'portfolio-categories\',
                                            \'field\'    => \'term_id\',
                                            \'terms\'    => $categoryOne
                                        ),
                                    )
                                );
                            $property = new WP_query( $args );

                            if( $property->have_posts() ) : ?>
                            <?php while( $property->have_posts() ) :
                            $property->the_post(); ?>
                            <article id="post-<?php the_ID(); ?>" class="post-catalog-news col-lg-6">
                                <a href="<?=get_permalink() ?>">
                                <?php
                               $size =  array( 370, 240);
                                the_post_thumbnail( $size ); ?>
                            <h2 class="title-catalog"><?php the_title(); ?></h2>
                                <? if (get_field(\'preview_post\')) { ?>
                                    <div class="preview_post_catalog">
                                        <? the_field(\'preview_post\') ?>
                                    </div>
                                <?  } ?>
                                </a>
                             </article>
                            <?php endwhile;
结束