Custom Ajax Query with template
-
I’m trying to create custom query with Ajax. Actually it’s working if I write custom code for title, links, featured image, description etc but If I want to use events calendar template, it’s not working.
global $post;
$search = strval( $_POST['search'] );
$posts_per_page = 2;
$paged = isset( $_POST['paged'] ) ? intval( $_POST['paged'] ) : 1;
$args = [
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
's' => $search,
'sentence' => true,
'ends_after' => 'now',
'paged' => $paged,
];
$events = tribe_get_events( $args );
// make 2nd query to get all relevant posts and use it in 'max_num_pages'
$found_args = $args;
$found_args['fields'] = 'ids';
$found_args['posts_per_page'] = -1;
$found_events = tribe_get_events( $found_args );
ob_start();
?>
<?php foreach ( $events as $event ) : ?>
<?php setup_postdata( $event ); ?>
/**************************************/
/***** This Template is not working ***/
<?php $this->template( 'list/event', [ 'event' => $event ] ); ?>
/**************************************/
<?php endforeach;
$content_html = ob_get_contents();
ob_end_clean();
wp_reset_query();
$response = array(
'content_html' => $content_html,
'max_num_pages' => ceil( count( $found_events ) / $posts_per_page ),
);
wp_send_json( $response );If I use include or similar, it also not working
$template_path = locate_template( 'tribe/events/v2/list/event.php' );
if ( !empty( $template_path ) ) {
include $template_path;
} else {
echo '<p>Event template not found.</p>';
}I already copied template file in the right directory. Query is working if I write a code:
echo $event->title;
but I need whole template with right class / div etc. I don’t want to create it manually. I need event calendar event template.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Custom Ajax Query with template’ is closed to new replies.