最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - wp_query with cat not working

matteradmin6PV0评论

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;

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far