简体   繁体   中英

WordPress get posts by category_name is not working

I need to List Posts order by category. I getting problem by display Posts by category No one is working for me, Like "Category_name, tag_id, cat, etc.."

$args1 = array(
    'taxonomy'=> 'portfolio_cat',
    'orderby' => 'slug',
    'order'   => 'ASC',
);

$cats = get_categories($args1);
foreach ($cats as $cat) 
{
  //echo $cat->slug;
  $args2 = array(
  'post_type' => 'advanced_portfolio',
  'posts_per_page' => -1,
   //'tag_id' => 25,
   // 'category_name' =>$cat->slug,
   'category_name' =>'video' //my category name **slug**
   );

   query_posts($args2);
   if (have_posts()) : 
   while (have_posts()) : the_post(); 
   the_title();
   endwhile;

   endif; 
}

You're using a custom post types which means it won't have regular categories but instead it will have a taxonomy which is used as a category, this means you need to use a tax query and also you need to check the terms, not tested but should work:

$taxonomy = 'portfolio_cat';
$cats = get_terms($taxonomy);

foreach ($cats as $cat) {
  $args1 = array(
    'post_type' => 'advanced_portfolio',
    'posts_per_page' => -1,
    'tax_query' => array(
      array(
        'taxonomy' => 'portfolio_cat',
        'field'    => 'slug',
        'terms'    => 'video'
      ),
    ),
  );

  query_posts($args1);

  if (have_posts()) : 
    while (have_posts()) : the_post(); 
      the_title();
    endwhile;
  endif;
}
wp_reset_query();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM