1

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?

1

2 Answers 2

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

-3

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;
1
  • 2
    It is good to write in a few words why the proposed code would solve the problem.
    – nmr
    Apr 9, 2019 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.