I have multiple wp_query's on the same page. Each one of those queries has an AJAX "read more" button at the bottom. The problem I'm finding is that I can only get one to work at a time. Whichever function is added first in the functions.php, that one works - the other one gets a 403 error for admin-ajax.php.
I'm pretty new to AJAX, so I've probably made a total hash of it, and I'm sure there's a way to combine this into a single function (preferred!) but if I can even just find a way for them all to work independently, that work be fine.
Here's my code:
The 2 WP_Queries in a custom page-work.php template:
First one:
<?php
//Find out how many posts
$total_posts = wp_count_posts('smart_maps');
$total_posts = $total_posts->publish;
$number_shown = 0;
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="smartmaps" class="smartmaps col xs-col-14 xs-offset-1 md-col-12 md-offset-2" style="display:none;">
<div class="in-here-smartmaps">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(get_the_ID());?>">
<div class="single-smartmap col xs-col-16 md-col-8">
<div class="col xs-col-8 xs-offset-4 md-col-4 md-offset-0 center">
<div class="image-element">
<img src="<?php the_field('thumbnail_image');?>" class="circle">
<div class="image-overlay circle"></div>
</div>
</div>
<div class="col xs-col-14 xs-offset-1 md-col-10 md-offset-1">
<h5 class="smart-map-title"><?php the_title();?></h5>
<?php if(get_field('icons')) :?>
<div class="block-icons">
<?php if(in_array('Heart', get_field('icons'))) :?>
<span class="icon-heart"></span>
<?php endif;?>
<?php if(in_array('Plant', get_field('icons'))) :?>
<span class="icon-plant"></span>
<?php endif; ?>
<?php if(in_array('Cup', get_field('icons'))) :?>
<span class="icon-cup"></span>
<?php endif;?>
<?php if(in_array('Book', get_field('icons'))) :?>
<span class="icon-book"></span>
<?php endif;?>
</div>
<?php endif;?>
</div>
</div>
</a>
<?php $number_shown++;
endwhile; ?>
<?php endif;?>
</div>
<?php if($number_shown != $total_posts) :?>
<div class="loadmore smarties col xs-col-14 xs-offset-1 md-col-8 md-offset-4 center">
<h3><a href="#">LOAD MORE</a></h3>
</div>
<?php endif;?>
</div>
<div clas="clearfix"></div>
The second 1:
<div id="strategic-events" class="strategicevents col xs-col-14 xs-offset-1 md-col-12 md-offset-2" style="display:none;">
<div class="in-here-strats">
<?php
//Find out how many posts
$total_posts = wp_count_posts('strategic_events');
$total_posts = $total_posts->publish;
$number_shown = 0;
$args = array( 'post_type' => 'strategic_events', 'posts_per_page' => 10, 'offset' => $the_offset );
$the_query2 = new WP_Query( $args ); ?>
<?php if ( $the_query2->have_posts() ) :
while ( $the_query2->have_posts() ) : $the_query2->the_post(); ?>
<a href="<?php echo get_permalink(get_the_ID());?>">
<div class="single-strategicevent col xs-col-16 md-col-8">
<div class="col xs-col-8 xs-offset-4 md-col-4 md-offset-0 center">
<div class="image-element">
<img src="<?php the_field('strategic_event_image');?>" class="circle">
<div class="image-overlay circle"></div>
</div>
</div>
<div class="col xs-col-14 xs-offset-1 md-col-10 md-offset-1">
<h5 class="strategic-event-title"><?php the_title();?></h5>
<?php if(get_field('subtitle')) :?>
<p><?php the_field('subtitle');?></p>
<small><?php the_field('location_text');?></small>
<?php endif;?>
</div>
</div>
</a>
<?php $number_shown++;
endwhile;
endif; ?>
</div>
<?php if($number_shown != $total_posts) :?>
<div class="loadmore strats col xs-col-14 xs-offset-1 md-col-8 md-offset-4 center">
<h3><a href="#">LOAD MORE</a></h3>
</div>
<?php endif;?>
</div>
<div class="clearfix"></div>
My JS code (both also in the page-work.php for now)...
<script>
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
$('body').on('click', '.loadmore.strats', function(e) {
e.preventDefault();
var data = {
'action': 'load_posts_by_ajax',
'page': page,
'security': '<?php echo wp_create_nonce("load_strats_posts"); ?>',
'max_page': '<?php global $wp_query; echo $wp_query->max_num_pages;?>'
};
$.post(ajaxurl, data, function(response) {
$('.in-here-strats').append(response);
page++;
});
});
});
<script>
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
$('body').on('click', '.loadmore.smarties', function(e) {
e.preventDefault();
var data = {
'action': 'load_posts_by_ajax',
'page': page,
'security2': '<?php echo wp_create_nonce("load_smartmaps_posts"); ?>',
'max_page': '<?php global $wp_query; echo $wp_query->max_num_pages;?>'
};
$.post(ajaxurl, data, function(response) {
$('.in-here-smartmaps').append(response);
page++;
});
});
});
</script>
And the AJAX functions in my function.php:
//Load More Smart Maps
add_action('wp_ajax_load_posts_by_ajax', 'load_smart_maps_by_ajax_callback');
add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_smart_maps_by_ajax_callback');
function load_smart_maps_by_ajax_callback() {
check_ajax_referer('load_smartmaps_posts', 'security2');
$paged = $_POST['page'];
$args = array(
'post_type' => 'smart_maps',
'post_status' => 'publish',
'posts_per_page' => '10',
'paged' => $paged,
);
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
?>
<?php
$total_posts = wp_count_posts('smart_maps');
$total_posts = $total_posts->publish;
$number_shown = $paged * 10 - 10;?>
<?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
<a href="<?php echo get_permalink(get_the_ID());?>">
<div class="single-smartmap col xs-col-16 md-col-8">
<div class="col xs-col-8 xs-offset-4 md-col-4 md-offset-0 center">
<div class="image-element">
<img src="<?php the_field('thumbnail_image');?>" class="circle">
<div class="image-overlay circle"></div>
</div>
</div>
<div class="col xs-col-14 xs-offset-1 md-col-10 md-offset-1">
<h5 class="smart-map-title"><?php the_title();?></h5>
<?php if(get_field('icons')) :?>
<div class="block-icons">
<?php if(in_array('Heart', get_field('icons'))) :?>
<span class="icon-heart"></span>
<?php endif;?>
<?php if(in_array('Plant', get_field('icons'))) :?>
<span class="icon-plant"></span>
<?php endif; ?>
<?php if(in_array('Cup', get_field('icons'))) :?>
<span class="icon-cup"></span>
<?php endif;?>
<?php if(in_array('Book', get_field('icons'))) :?>
<span class="icon-book"></span>
<?php endif;?>
</div>
<?php endif;?>
</div>
</div>
</a>
<?php $number_shown++;
if ( $number_shown == $total_posts ) {
echo '<style>.loadmore.smarties {display:none;}</style>';
}
endwhile ?>
<?php endif;
wp_die();
}
//Load More Strategic Events
add_action('wp_ajax_load_posts_by_ajax', 'load_strats_by_ajax_callback');
add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_strats_by_ajax_callback');
function load_strats_by_ajax_callback() {
check_ajax_referer('load_strats_posts', 'security');
$paged = $_POST['page'];
$args = array(
'post_type' => 'strategic_events',
'post_status' => 'publish',
'posts_per_page' => '10',
'paged' => $paged,
);
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
?>
<?php
$total_posts = wp_count_posts('strategic_events');
$total_posts = $total_posts->publish;
$number_shown = $paged * 10 - 10;?>
<?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
<a href="<?php echo get_permalink(get_the_ID());?>">
<div class="single-strategicevent col xs-col-16 md-col-8">
<div class="col xs-col-8 xs-offset-4 md-col-4 md-offset-0 center">
<div class="image-element">
<img src="<?php the_field('strategic_event_image');?>" class="circle">
<div class="image-overlay circle"></div>
</div>
</div>
<div class="col xs-col-14 xs-offset-1 md-col-10 md-offset-1">
<h5 class="strategic-event-title"><?php the_title();?></h5>
<?php if(get_field('subtitle')) :?>
<p><?php the_field('subtitle');?></p>
<small><?php the_field('location_text');?></small>
<?php endif;?>
</div>
</div>
</a>
<?php $number_shown++;
if ( $number_shown == $total_posts ) {
echo '<style>.loadmore.strats {display:none;}</style>';
}
endwhile ?>
<?php endif;
wp_die();
}
I'm all up for simplifying this into a single function if possible, or if not, at least getting it to work independently. I'm guessing that it's only viewing 1 wp_nonce's and checking it against both AJAX calls, so the first one it reads is fine, the second is coming up with a 403?
/wp-json/wp/v2/posts
to both render the initial posts, and pull in additional pages. Nonces etc shouldn't be necessary