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?
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?
Share Improve this question asked May 15, 2020 at 15:27 ChristinaChristina 196 bronze badges1 Answer
Reset to default 1Been 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.