0

I am trying to print all posts from all categories but the posts to show within category at this moment its printing all the posts below all categories. Here is the code. Yo can use the shortcode [bpo_microsoft_shortcode]. Thanks in advance:

        <?php
        function Custom_Post_Type_Grouped_By_Category(){ 
            global $post;
            $cat_args = array(
                'orderby'       => 'term_id', 
                'order'         => 'ASC',
                'hide_empty'    => true, 
            );    
            $terms = get_terms('Custom Post Type Reg', $cat_args);
            $content = ' ';
            $content .= '<ul >';
                foreach($terms as $taxonomy){
                    $term_slug = $taxonomy->slug;
                    $cat_id = $taxonomy->term_id;
                    $tax_post_args = array(
                          'post_type' => 'Custom Post Type',
                          'posts_per_page' => 999,
                          'order' => 'ASC',
                          'numberposts' => -1,
                          'tax_query' => array(
                                array(
                                     'taxonomy' => 'Custom Post Type Reg',
                                     'field' => 'slug',
                                     'terms' => $term_slug,
                                     'id'    => $cat_id
                                ),
                           )
                    );    
                      $content .= '<li >'; 
                        $content .= '<div >';         
                            $content .= $taxonomy->name;
                                $the_query = null;
                                $the_query = new WP_Query( $tax_post_args );  
                                if ( $the_query->have_posts() ) {                           
                                    $content .= '<ul >';
                                    while ( $the_query->have_posts() ) {
                                        $the_query->the_post();
                                        $content .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
                                    }                     
                                    $content .= '</ul>';
                                }             
                                wp_reset_postdata();       
                         $content .= '</div>';    
                      $content .= '</li>';           
                    } //end foreach loop
            $content .= '</ul>';
            return $content;

        }
        add_shortcode('shortcode', 'Custom_Post_Type_Grouped_By_Category');
        ?>
1

From the it looks like you want to get all products grouped by category. Your query seems to be correct except $term_slug need not be in quotes.

$tax_post_args = array(
                  'post_type' => 'bpo_microsoftoffice',
                  'posts_per_page' => 999,
                  'order' => 'ASC',
                  'tax_query' => array(
                        array(
                             'taxonomy' => 'bpo_microsoftoffice_reg',
                             'field' => 'slug',
                             'terms' => $term_slug
                        )
                   )
            ); 

Edit:

Method 1:

If you want just query you wrote, use WP_Query($tax_post_args );.

Method 2:

If you want both category plus custom_taxonomy, add 'category' => $cat_id, in $tax_post_args.

So,

$tax_post_args = array(
              'post_type' => 'bpo_microsoftoffice',
              'posts_per_page' => 999,
              'order' => 'ASC',
              'tax_query' => array(
                    'relation' => 'OR',
                    array(
                         'taxonomy' => 'bpo_microsoftoffice_reg',
                         'field' => 'slug',
                         'terms' => $term_slug
                    ),
                    array(
                         'taxonomy' => 'category',
                         'field' => 'slug',
                         'terms' => $cat_id
                    ),
               )
        ); 

And the use WP_Query($tax_post_args);

This is assuming your post_type is having category as well as bpo_microsoftoffice_reg as taxonomies. If only bpo_microsoftoffice_reg is assigned, use first method.

  • I did this 'terms' => '$term_slug' but its still printing all posts on all categories :-( – mysticalghoul Sep 19 '16 at 6:50
  • Try it without ' ' around $term_slug – Aniruddha Gawade Sep 19 '16 at 6:51
  • If it doesn't work, print_r($the_query) and check if query is proper. – Aniruddha Gawade Sep 19 '16 at 6:53
  • Seems to be correct. but its not grouping – mysticalghoul Sep 19 '16 at 7:05
  • Just noticed $the_query = new WP_Query( array( 'category' => $cat_id, 'post_type' => 'bpo_microsoftoffice' ) ); . You're supposed to pass $tax_post_args to WP_Query() – Aniruddha Gawade Sep 19 '16 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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