Reputation:
I am having some loadmore issues with ajax. Who can help me.
I have code
<button class="load-more">More</button>
Function
add_action('wp_ajax_loadmore', 'get_post_loadmore');
add_action('wp_ajax_nopriv_loadmore', 'get_post_loadmore');
function get_post_loadmore() {
$offset = isset($_POST['offset']) ? (int)$_POST['offset'] : 0;
$getposts = new WP_query(); $getposts->query('post_type=event&post_status=publish&showposts=5&offset='.$offset);
global $wp_query; $wp_query->in_the_loop = true;
while ($getposts->have_posts()) : $getposts->the_post(); ?>
<?php do_action('postthumb1')?>
<?php endwhile; wp_reset_postdata();
die();
}
js
<script>
$(document).ready(function(){
var offset = 4;
$('.load-more1').click(function(event) {
$.ajax({
type : "post",
dataType : "html",
async: false,
url : '<?php echo admin_url('admin-ajax.php');?>',
data : {
action: "loadmore",
offset: offset,
},
beforeSend: function(){
},
success: function(response) {
$('.list-new').append(response);
offset = offset + 5;
},
error: function( jqXHR, textStatus, errorThrown ){
console.log( 'The following error occured: ' + textStatus, errorThrown );
}
});
});
});
</script>
Code working ok but, it load only post_type
event. I want to set a variable that can be used for all custom post types.
I tried setting the variable but the code does not work. Does anyone who has experience with ajax can help me?
Upvotes: 0
Views: 391
Reputation: 830
This will get you all custom post types on the site:
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
$args = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => 5,
'offset' => $offset
);
$getpost = new WP_Query( $args );
If you're looking for a certain post_type passed in by the ajax call:
<script>
$(document).ready(function(){
var offset = 4;
$('.load-more1').click(function(event) {
$.ajax({
type : "post",
dataType : "html",
async: false,
url : '<?php echo admin_url('admin-ajax.php');?>',
data : {
action: "loadmore",
offset: offset,
posttype: posttype //Where ever you want to get this from
},
beforeSend: function(){
},
success: function(response) {
$('.list-new').append(response);
offset = offset + 5;
},
error: function( jqXHR, textStatus, errorThrown ){
console.log( 'The following error occured: ' + textStatus, errorThrown );
}
});
});
});
</script>
Function:
add_action('wp_ajax_loadmore', 'get_post_loadmore');
add_action('wp_ajax_nopriv_loadmore', 'get_post_loadmore');
function get_post_loadmore() {
$offset = isset($_POST['offset']) ? (int)$_POST['offset'] : 0;
$posttype = isset($_POST['posttype']) ? $_POST['posttype'] : 'post';
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => 5,
'offset' => $offset
);
$getposts = new WP_query($args);
global $wp_query;
$wp_query->in_the_loop = true;
while ($getposts->have_posts()) : $getposts->the_post();
do_action('postthumb1');
endwhile;
wp_reset_postdata();
die();
}
Upvotes: 1