I have created a shortcode to show custom single custom post type to another post. Here is the code:
function clients_shortcode(){
$output = '';
//Attributes
$defaults = array(
'c_name' => '',
'c_session' => ''
);
$atts = shortcode_atts($defaults, $atts);
$name = $atts['c_name'];
$session = $atts['c_session'];
$args = array(
'post_type' => 'client',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'client_name',
'value' => $name,
'compare' => '='
),
array(
'key' => 'session_number',
'value' => $session,
'compare' => '='
)
)
);
$client_sess = new WP_Query ($args);
if($client_sess->have_posts()): while($client_sess->have_posts()): $client_sess -> the_post();
$output .= '<h1>' . get_the_title() . '</h1>';
$output .= '<div>' . get_field('session_hypnosis') . '</div>';
$output .= '<div>' . get_field('session_video') . '</div>';
endwhile;
endif;
wp_reset_query();
return $output;
}
add_shortcode('clients', 'clients_shortcode');
The output is showing nothing but if I change the code to static like this
$args = array(
'post_type' => 'client',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'client_name',
'value' => 'Nayan',
'compare' => '='
),
array(
'key' => 'session_number',
'value' => 1,
'compare' => '='
)
)
);
The shortcode then show the post for client Nayan and session 1. But when I am passing arguments to the shortcode, it is not working.
NOTE: I am using ACF to generate custom fields.
Thanks In Advance