Skip to content
Advertisement

Load More Custom Posts On Click Ajax WordPress

I’m trying to create a load more button that loads more custom post types on click, however, I keep receiving a 400 (Bad Request) error. Here is the code i’m using.

JavaScript

    

JavaScript

add_action(‘wp_ajax_nopriv_more_post_ajax’, ‘more_post_ajax’); add_action(‘wp_ajax_more_post_ajax’, ‘more_post_ajax’);

Advertisement

Answer

There is a good amount here to fix.

Your Ajax is syntax is off a bit. Also, you can specify posts per page for the return with PHP, so you don’t need to send that, you can keep your page attribute on the frontend.

JavaScript

For your initial PHP function, you should only specify the amount of posts you want to show at first. If you use -1 it will just show all posts at the start, which you don’t want. You also don’t need to use the paged parameter. You only need offsets.

JavaScript

For your more posts function, you only need to specify an offset, and can add your additional posts right in that function.

JavaScript

I have not tested this, but this should get you closer to a working set.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement