The arguments for your myCPT()
function are incorrect. Callbacks used for add_shortcode()
received two arguments, in this order:
- An array that contains the parameters used with the shortcode.
- The that is between the opening and closing shortcode, if supported.
You code is assigning the attributes to $query
(and then overwriting them), and trying to get the attributes from the shortcode content.
So you code needs to be:
function myCPT( $atts, $content ) {
$option = shortcode_atts( array( 'cat' => '' ), $atts ); // This needs to be the same as the first argument.
$query = new WP_query( array(
'post_type' => 'myposttype',
'category_name' => $option['cat']
) );
// etc.
}
add_shortcode( 'myshortcode', 'myCPT' );