You just want to make sure you get all your default attributes set in addition to only adding tax queries when you have all the values set.
<?php
// [loop post_type="website" taxonomy="industry" terms="beauty" posts="3"][/loop]
// [loop post_type="website" posts="-1" ][/loop]
function loop_recent_posts_shortcode( $atts ) {
$a = shortcode_atts(
array (
'posts' => 6,
'post_type' => '',
'taxonomy' => '',
'terms' => '',
'meta_key' => '',
'meta_value' => 0,
), $atts );
$args = array (
'post_type' => array ( $a[ 'post_type' ] ),
'orderby' => 'date',
'order' => 'DESC',
'showposts' => $a[ 'posts' ],
'fields' => 'ids',
);
// only add if we have both keys in the original values
if ( isset( $atts[ 'meta_key' ], $atts[ 'meta_value' ] ) ) {
$args[ 'meta_key' ] = $a[ 'meta_key' ];
$args[ 'meta_value' ] = $a[ 'meta_value' ];
}
// only add if we have both keys in the original values
if ( isset( $atts[ 'taxonomy' ], $atts[ 'terms' ] ) ) {
$tax_queries = array (
array (
'field' => 'slug',
'taxonomy' => $a[ 'taxonomy' ],
'terms' => $a[ 'terms' ],
),
);
$args[ 'tax_query' ] = $tax_queries;
}
// run the query
$q = new WP_Query( $args );
$content="";
$content .= '<ul class = "work_loop">' . PHP_EOL;
if ( $q->have_posts() ) :
while( $q->have_posts() ) : $q->the_post();
// THUMBNAIL
if ( has_post_thumbnail( get_the_ID() ) ) {
$thumnail_src = function_exists( 'ml_get_thumbnail' ) ? ml_get_thumbnail( null, '3_col_folio' ) : get_the_post_thumbnail_url( null, '3_col_folio' );
$content .= '<li><a href="' . get_the_permalink();
$content .= '"><span class="icon more_icon"></span><img src="' . $thumnail_src
. '" alt="websites"><span class="triangle_up"></span><h2>'
. get_the_title() . '</h2></a></li>' . PHP_EOL;
}
else {
// NO THUMBNAIL
$content .= '<li><a href="' . get_the_permalink();
$content .= '"><h2>' . get_the_title() . '</h2></a></li>' . PHP_EOL;
}
endwhile; // the_post()
else:
//... no posts
endif; // have_posts()
wp_reset_postdata();
$content .= '</ul>';
return $content;
}
add_shortcode( 'loop', 'loop_recent_posts_shortcode' );