WP_Query Custom Post Type Query by Category Variable
Sep 10, 2020 | WordPress
// Shortcode for Custom Post Types [xxx_shortcode category="post-category"]
function xxx_query($atts){
ob_start();
$var = shortcode_atts( array(
'category' => 'y',
), $atts );
$args = array(
'post_type' => 'post',
'tax_query' => array(array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $var['category']))
);
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()){
$query->the_post();
}
}else{
echo 'No Posts Available.';
}
return ob_get_clean();
}
add_shortcode('xxx_shortcode', 'xxx_query');
Query a custom post type by category using a variable in the shortcode.