php - wp_query with cat not working

I am trying to retrieve some custom post type from their specific parent category.<div class="tab-content"&

I am trying to retrieve some custom post type from their specific parent category.

 <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>

if I remove, in the array $args, the key 'cat', it will work but it will output ALL my menu. What I want is to be able to output my menu post type from the category_id, but I have nothing that shows up. So I guess $recipes have nothing in it, then fails.

Is there something I missed out to retrieve custom post type with a specific category id?

I am trying to retrieve some custom post type from their specific parent category.

 <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>

if I remove, in the array $args, the key 'cat', it will work but it will output ALL my menu. What I want is to be able to output my menu post type from the category_id, but I have nothing that shows up. So I guess $recipes have nothing in it, then fails.

Is there something I missed out to retrieve custom post type with a specific category id?

Share Improve this question asked Aug 3, 2015 at 18:28 Matthieu BoisjoliMatthieu Boisjoli 1134 bronze badges 1
  • why don't you use Taxonomy Parameters like we have in codex – Zak Commented Aug 3, 2015 at 18:44
Add a comment  | 

2 Answers 2

Reset to default 4

From what u have written so far it seems menu_categorie is the custom taxonomy. Make sure it is this only. I get a feeling that you have misses "s" from the end of menu_categorie

Anyways

The argument 'cat' => $id you were using is used for default taxonomy i.e category.

But in your case you have custom taxonomy menu_categorie.

So you need to use the tax_query.

So this is how your code will look.

<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>

This should work . If it doesn't please let me know

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745611786a4635988.html

相关推荐

  • php - wp_query with cat not working

    I am trying to retrieve some custom post type from their specific parent category.<div class="tab-content"&

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信