How to display all posts category wise of a custom post type

I am working on a institute website which as courses.
I want to display all posts as list category wise of a custom post type.
for Ex.:

CATEGORY NAME1
– Post of category name 1
– Post of category name 1

Read More

CATEGORY NAME2
– Post of category name 2
– Post of category name 2

and so on

below is the code which is displaying all posts from custom post type from all categories but i want to show them category wise seperately please help me…

<?php
$type = 'course';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1);


$my_query = '';
$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();
?>

Related posts

2 comments

  1. you may need to do your query more than once, or one per category the key is adding ‘category_name’ => ‘slug_name’ to your query

        <?php
    // query category 1   
     $type = 'course';
        $args1=array(
          'post_type' => $type,
          'post_status' => 'publish',
          'posts_per_page' => -1,
        'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
          'caller_get_posts'=> 1);
    
    // query category 2   
     $type = 'course';
        $args2=array(
          'post_type' => $type,
          'post_status' => 'publish',
          'posts_per_page' => -1,
        'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
          'caller_get_posts'=> 1);
    
        $my_query = '';
        $my_query = new WP_Query($args1);
        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();
    
       $my_query = '';
        $my_query = new WP_Query($args2);
        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();
        ?>

  2. 100% working this code

    $tax_query[] = array(
                     'taxonomy'      => '< taxonomy_slug >',
                    'field' => 'term_id', 
                    'terms'         => < terms_ids >, // array(12,13,...)
    
                );
    
                $args = array(
                    'post_type'      => '< post type name>',
                    'posts_per_page' => 10,
                    'tax_query' => $tax_query
                );
    
              $loop = new WP_Query( $args );
              while ( $loop->have_posts() ) : $loop->the_post();
                echo  get_the_ID();
    
              endwhile;
    

Comments are closed.