admin_url('admin-ajax.php'), 'ajax_nonce' => wp_create_nonce('load_more_post_nonce') ]); } } add_action('wp_enqueue_scripts', 'add_ajax_scripts'); /* --------------------------- LOAD MORE BROCHURES ---------------------------*/ add_action('wp_ajax_get_more_brochures', 'load_more_brochures'); add_action('wp_ajax_nopriv_get_more_brochures', 'load_more_brochures'); function load_more_brochures() { $offset = $_POST['ajax_data']['offset']; // $data['user1'] = 'server response user1'; // $data['user2'] = 'server response user2'; // wp_send_json_success($data); $args = array( 'post_type' => 'brochures', 'posts_per_page' => 1, 'post_status' => 'publish', 'offset' => $offset, 'order' => 'DESC', 'orderby' => 'date', 'meta_key' => 'brochure_pdf', 'meta_value' => '', 'meta_compare' => '!=' ); $ajax_query = new WP_Query($args); // $isQueryEmpty = $ajax_query->have_posts() ? false : true; $isQueryEmpty = $offset + 1 >= $ajax_query->max_num_pages ? true : false; // BREAK IF QUERY IS EMPTY if ($ajax_query->have_posts() == false) { wp_send_json(array('data' => '', 'isQueryEmpty' => $isQueryEmpty)); die(); } ob_start(); if ($ajax_query->have_posts()) : while ($ajax_query->have_posts()) : $ajax_query->the_post(); get_template_part('template-components/archives/brochure-grid-row', null, array('ID' => get_the_ID())); endwhile; endif; // ob_end_flush(); $response = ob_get_clean(); wp_send_json(array('data' => $response, 'isQueryEmpty' => $isQueryEmpty)); die(); } /* --------------------------- LOAD MORE FICHES INFOS ---------------------------*/ add_action('wp_ajax_get_more_fiches_infos', 'load_more_fiches_infos'); add_action('wp_ajax_nopriv_get_more_fiches_infos', 'load_more_fiches_infos'); function load_more_fiches_infos() { $offset = $_POST['ajax_data']['offset']; $args = array( 'post_type' => 'fiches-infos', 'posts_per_page' => 1, 'post_status' => 'publish', 'offset' => $offset, 'order' => 'DESC', 'orderby' => 'date', 'meta_key' => 'brochure_pdf', 'meta_value' => '', 'meta_compare' => '!=' ); $ajax_query = new WP_Query($args); // $isQueryEmpty = $ajax_query->have_posts() ? false : true; $isQueryEmpty = $offset + 1 >= $ajax_query->max_num_pages ? true : false; // write_log($isQueryEmpty); // BREAK IF QUERY IS EMPTY if ($ajax_query->have_posts() == false) { wp_send_json(array('data' => '', 'isQueryEmpty' => $isQueryEmpty)); die(); } ob_start(); if ($ajax_query->have_posts()) : while ($ajax_query->have_posts()) : $ajax_query->the_post(); get_template_part('template-components/archives/brochure-grid-row', null, array('ID' => get_the_ID())); endwhile; endif; // ob_end_flush(); $response = ob_get_clean(); wp_send_json(array('data' => $response, 'isQueryEmpty' => $isQueryEmpty)); die(); }