0
votes

EDIT: After changing the tax_query to slug it now outputs posts in the first loop (the first cat) but does not in the next tab/cat which I know has many published posts assigned to it.

I created a shortcode that is meant to grab all the categories in a CPT and out them into bootstrap tabs with a certain amount of posts per tab.

To do this I do get_categories for the tabs nav (Works fine). Then I do get_categories again to output the tabs content (Also works fine) with a wp_query inside each where I grab the posts. This is what is not working. No output.

If I echo the category name or any data I can grab from the get_categories it echo's it. So it seems the issue is with the WP_Query.

Here is the code:

add_shortcode( 'tabbed_grid_articles', 'tabbed_grid_articles' );
function tabbed_grid_articles($args = array()){
    global $post;
    $current_id = $post->ID;
    $defaults = apply_filters( 'dy_tabbed_grid_default_args', array(
        'posts_per_page'   => 1,
        'orderby'       => 'date',
        'order'         => 'DESC',
        'echo'          => true,
        'post_type'     => 'playbooks',
        'category'      => 0,
        'hide_empty'    => true,
        'exclude'       => '',
        'include'       => '',
        'taxonomy'      => 'playbooks_category',

    ));

    $args = wp_parse_args( $args, $defaults );

    $cat_args = array(
        'taxonomy'      => $args['taxonomy'],
        'orderby'       => 'menu_order',
        'order'         => 'ASC',
        'hierarchical'  => true,
        'parent'        => $args['category'],
        'hide_empty'    => $args['hide_empty'],
        'child_of'      => $args['category'],
        'exclude'       => $args['exclude'],
        'include'       => $args['include']
    );

    $categories = get_categories($cat_args);
    ?> <ul class="nav nav-tabs" role="tablist"> <?php
        $count = 0;
        foreach($categories as $category){
            $targetID = str_replace(' ', '-', $category->cat_name);
            ?>        <!-- Nav tabs -->
            <li role="presentation" class="<?php if($count == 0){echo 'active'; $count ++;} ?>"><a href="#tab-<?php echo $targetID ?>" aria-controls="tab-<?php echo $targetID ?>" role="tab" data-toggle="tab"><?php echo $category->cat_name ?></a></li>
            <?php
        }
       // wp_reset_postdata();
        ?>     </ul>
    <!-- Tab panes -->
    <div class="tab-content">
        <?php
        $count = 0;
        foreach($categories as $category) {
            $targetID = str_replace(' ', '-', $category->cat_name);
            $args['tax_query'] = array(array(
                    'taxonomy' => $args['taxonomy'],
                    'field' => 'slug',
                    'terms' => $category->cat_name,
                ),
            );
//            $args['cat'] = $category->cat_ID;
            $grid_query = new WP_Query($args);
            ?>
            <div role="tabpanel" class="tab-pane <?php if($count == 0){echo 'active'; $count ++;} ?>" id="tab-<?php echo $targetID ?>">
                <?php
                if($grid_query->have_posts()) {
                    while ($grid_query->have_posts()) {
                        $grid_query->the_post();
                        echo get_the_title();
                        //echo '1<br>';
                        //echo $args['cat'];
                        //echo $category->cat_name;
                        //echo get_the_excerpt();
                        echo '<br>';
                        //the_excerpt();
                    }
                    wp_reset_postdata();
                }
                ?></div>
            <?php
        }
        ?>



    </div>

    <?php
}

I tried $args['cat'] = $category->cat_ID; instead of tax_query but neither work. If I comment them both out then I get all the posts from the cpt. Currently just echoing the excerpt...

Any ideas?

1
Please excuse some of the junk data in the like 1<br> and echo $args['cat'], it is just to see if it is counting the posts which it is not when I try to manipulate the category.Allon

1 Answers

0
votes

Solved. I took for granted that the slug was the same as the name and it was not. I think I only changed the slug to be the name by mistake when debugging at first when it was not working. Anyway, changed 'terms' => $category->cat_name, to 'terms' => $category->slug, and it worked.

Hope this helps someone in the future.