在 Wordpress 中使用自定义帖子类型时,无法在循环中过滤类别


Can't filter on category in loop when using Custom Post Type in Wordpress

我正在尝试使用以下代码显示名为 portfolio 的自定义帖子类型的帖子,然后按类别过滤结果,我尝试将 - category_name、catid、portfolio_category 放在数组中

浏览了一下论坛并尝试了一些事情,但似乎无法让它工作,它要么不显示任何内容,要么显示所有类别的所有帖子。

     <?php
     $args=array(
    'post_type' => 'portfolio',
    'post_status' => 'publish',
    'posts_per_page' => 7,
    'caller_get_posts'=> 1
     );
     $my_query = null;
     $my_query = new WP_Query($args);
     if( $my_query->have_posts() ) {
     while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
     <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
     <?php
      endwhile;
      }
     wp_reset_query();  // Restore global post data stomped by the_post().
     ?>
注册分类法

portfolio_category,任何帮助将不胜感激,非常感谢

您可以尝试将该类别称为自定义分类:

'portfolio_cat' => 'name_of_your_category'

在尝试了一些事情之后,这就是我如何使用"tax_query"让它工作的方式,希望这对某人有所帮助......

     <?php
     $args = array(
     'post_type'=>'portfolio',
  'tax_query' => array(
    array(
        'taxonomy' => 'portfolio_category',
        'field' => 'slug',
        'terms' => 'solutions'
    )
   )
       );
     $my_query = new WP_Query( $args );
     if( $my_query->have_posts() ) {
     while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
     <?php the_title_attribute(); ?>">
     <?php the_title(); ?></a></p>
      <?php the_excerpt()?>
      <?php echo get_the_post_thumbnail($post->ID,  array(50,50)); ?> 
      <?php
      endwhile;
      }
     wp_reset_query();  // Restore global post data stomped by the_post().
     ?>