Search code examples
phpwordpressforeachcustom-post-type

How to fix 'custom post show by category' in WordPress


How can I fix this problem? I'm trying with passing a variable in WP_Query on 'category_name' => $category->slug, but not showing any posts in output. When I remove 'category_name' => $category->slug, line from code then showing posts but need to display posts by category.

<?php
function mytheme_schedule_shortcode($atts, $content = null){
    extract( shortcode_atts( array(
        'style'         => 1
    ), $atts) );
    $domain = 'mytheme-toolkit';
    $mytheme_schedule_categories = get_terms('schedule_cat');

    $mytheme_schedule_markup ='
        <section class="schedule-area bg-image ptb-120">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="tab">
                            <ul class="tabs">';
                            if (!empty($mytheme_schedule_categories) && ! is_wp_error($mytheme_schedule_categories)) {
                                foreach($mytheme_schedule_categories as $category){
                                $mytheme_schedule_markup .='
                                <li>
                                    <a href="#">'.esc_html__($category->name).'
                                        <span>'.esc_html__($category->slug).'</span>
                                    </a>
                                </li>'; 
                                }   
                            } 
                            $mytheme_schedule_markup .=' 
                            </ul>

                            <div class="tab_content">';
                                foreach($mytheme_schedule_categories as $category){
                                    $mytheme_schedule_markup .='
                                    <div class="tabs_item">
                                        <ul class="accordion">';
                                            $args = array(
                                                'post_type' => 'schedule',
                                                'posts_per_page' => -1,
                                                'category_name' => $category->slug,
                                            );
                                            $schedule_array = new WP_Query( $args );

                                            while($schedule_array->have_posts()): $schedule_array->the_post();
                                                $mytheme_schedule_markup .= '
                                                <li class=-item">
                                                        <h3>'.get_the_title().'</h3>
                                                </li>';

                                            endwhile;
                                            wp_reset_query();
                                            $mytheme_schedule_markup .= ' 
                                        </ul>   
                                    </div>';
                                }
                                $mytheme_schedule_markup .='
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>';
    return $mytheme_schedule_markup;
}
add_shortcode('mytheme_schedule', 'mytheme_schedule_shortcode');

Solution

  • It appears that you are using a custom taxonomy for your post type in which case the code should be something like this:

    $args = array(
      'post_type' => 'schedule',
      'posts_per_page' => -1,
      'tax_query' => array(
        array(
            'taxonomy' => 'schedule_cat',
            'field'    => 'slug',
            'terms'    => $category->slug,
        ),
      ),
    );
    $schedule_array = new WP_Query( $args );
    

    This should resolve the issue for you. Good Luck!!!

    For future reference, check the WP Query arguments here