0

I'm trying to build a shortcode that will allow the user to choose what category is displayed and how many posts will show. The shortcode worked fine until I added the 'tax_query'

add_shortcode( 'latest_post', 'latest_post_query_shortcode' );
function latest_post_query_shortcode(  $atts ) {
    $atts = shortcode_atts( array(
            'post_per_page' => '',
            'category' => '',
    ), $atts );

    $args = array(
            'post_type'     => 'post',
            'post_status'   => 'publish',
            'posts_per_page'=> 3,
            'tax_query'     => array( array(
                                'taxonomy'  => 'category',
                                'field'     => 'slug',
                                'terms'     => $categories
                            ), ),
        );

        $string = '';
    // The Query
    $query = new WP_Query( $args ); 

    // The Loop
    if ( $query->have_posts() ) { ?>
        <section class="recent-posts clear">
        <?php while ( $query->have_posts() ) : $query->the_post() ; ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
                <? echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
                    echo get_the_title( $_post->ID);
                    echo '</a>';
                    echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
                    echo get_the_post_thumbnail( $_post->ID, 'medium' );
                    echo '</a>';
                    echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
                    echo '<button>Read More</button>';
                    echo '</a>';
                ?>

            </article>
        <?php endwhile; ?>
        </section>
    <?php } else {
        echo 'No posts found';
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}

Before adding the tax_query, it would show the three latest posts, with the tax_query, it just shows "No posts found." What am I doing wrong here?

1 Answer 1

1

Been a while since I used shortcode_atts but should $categories be $category? also, make sure it's returning the correct type e.g. string or int and you're searching for a valid slug.

10
  • I tried that (category vs categories), thinking the same thing but unfortunately it wasn't it. The slugs I've tested are valid. How would I check what it's supposed to be returning?
    – Christina
    May 15, 2020 at 19:45
  • If you're passing a slug then it should be fine, it's more if you were passing an id where it would probably be a string instead of an int but basically do a var_dump($categories) right before the $args = array( ... so that you can see what it's value is. It would also be worth sharing the shortcode you're using to test this.
    – Bob
    May 15, 2020 at 19:51
  • I'm getting NULL from the var_dump. Shortcode is: [latest_post category="drinks"] 31 posts under the "drinks" slug imgur.com/EyvJSEH
    – Christina
    May 15, 2020 at 20:29
  • Should definitely say $category then, what do you get if you var_dump($category);?
    – Bob
    May 15, 2020 at 20:52
  • 1
    Thank you Bob! Switching it to $atts['category'] was it.
    – Christina
    May 19, 2020 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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