I see two problems here. One is that the explode()
doesn’t actually specify the correct split string, which is ', '
, WITH a space.
What is written in the sample:
'post_type' => explode( ',', $posttype ),
What is written in the example shortcode:
[myquery posttype="cpt_press, cpt_two, cpt_three, cpt_etc"]
Note that there is a comma AND a space between each post type. I would use preg_split()
instead to allow for some flexibility in the way this value could be written in the shortcode:
'post_type' => preg_split( '/\s*,\s*/', $posttype ),
This will allow any amount of white-space to be used on either side of each comma between the post types in the given list string.
The second problem I see is that the given code is testing the $posttype
variable for equality to ”cpt_press’`, even though it could be a comma-separated list.
Instead, check for membership of 'cpt_press'
in the $args['post_type']
, array, which will set to the list of the post types given in the posttype
shortcode argument:
in_array( 'cpt_press', $args['post_type'] )
The updated code would thus be:
function myshortcode( $params, $content = null ) {
global $post;
extract( shortcode_atts( array(
'posttype' => '',
'meta_key' => '',
'priority' => '',
'meta_compare' => '',
'layout' => 'rows',
'cols' => 1,
'tag' => '',
'count' => 10,
'orderby' => 'date',
'order' => 'DESC'
), $params ) );
$args = array(
'post_type' => preg_split( '/\s*,\s*/', $posttype ),
);
$myquery = new WP_Query( $args );
ob_start();
?><div class="row"><?php
// The Loop
if ( $myquery->have_posts() ) : while( $myquery->have_posts() ) : $myquery->the_post();
if ( in_array( 'cpt_press', $args['post_type'] ) ) :
the_content();
else :
the_excerpt();
endif;
endwhile;
endif;
wp_reset_postdata();
?></div><?php
return ob_get_clean();
}
add_shortcode( 'myquery', 'myshortcode' );