I believe the problem is you are not calling do_shortcode()
in the output buffer. You don’t need to be running the loop within the output buffer since I can assume get_content_template()
simply returns HTML with embedded shortcodes. Save that to a string variable, then run that through the do_shortcode()
and save the echo
ed output to the buffer.
This is untested but should steer you in the right direction:
function theme_load_more_posts() {
check_ajax_referer( 'theme-load-more-posts-nonce', 'nonce' );
$args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
$args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
$args['paged'] = esc_attr( $_POST['page'] );
$args['post_status'] = 'publish';
$loopContent="";
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post();
$loopContent .= get_content_template();
endwhile;
endif;
ob_start();
echo do_shortcode($loopContent);
$data = ob_get_clean();
wp_send_json_success( $data ); // Performing do_shortcode here but it doesn't work.
wp_reset_postdata();
wp_die();
}
add_action( 'wp_ajax_theme_load_more_posts', 'theme_load_more_posts' );
add_action( 'wp_ajax_nopriv_theme_load_more_posts', 'theme_load_more_posts' );