1

I'm trying to replace the standard WP pagination with an Ajax load more posts button. I'm basically following the code from this question which for the most part works well.

Here's the relevant code:

HTML (blog index page):

<div id="content">

<?php $args = array(
    'posts_per_page' => 5
);

if (isset($_GET['views'])) {
  $args['orderby'] = 'meta_value_num';
  $args['meta_key'] = 'post_views_count';
  $args['order'] = 'DESC';
};

$wp_query = new WP_Query($args); ?>

<?php while( $wp_query->have_posts() ): $wp_query->the_post();  ?>
    <?php the_title(); ?>
<?php endwhile; ?>  

<a id="more_posts">Load More</a>

<?php wp_reset_postdata(); ?>

</div>

JS (at the bottom of blog index page):

var ajaxUrl = "<?php echo admin_url('admin-ajax.php', null); ?>";
var page = 1; // What page we are on.
var ppp = 4; // Post per page

jQuery("#more_posts").on("click",function(){ // When btn is pressed.
    jQuery("#more_posts").attr("disabled",true); // Disable the button, temp.
    jQuery.post(ajaxUrl, {
        action:"more_post_ajax",
        offset: (page * ppp) + 1,
        ppp: ppp
    }).success(function(posts){
        page++;
        jQuery("#content").append(posts);
        jQuery("#more_posts").attr("disabled",false);
    });

   });

PHP (in functions.php):

function more_post_ajax(){

$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
header("Content-Type: text/html");

$args2 = array(
    'posts_per_page' => $ppp,
    'offset' => $offset,
    'orderby' => 'rand'
);

$custom2 = new WP_Query($args2);

while ($custom2->have_posts()) : $custom2->the_post(); 
   the_title();
endwhile;

exit;
}

add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax'); 
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');

What I need to do is alter the parameters of the query used to load the posts based on whether 'views' is selected as the order, as it works in the template file's code.

Everything works as its should, except that arguments added to the query in functions.php are not working correctly. For example, if I change posts_per_page from the $ppp variable to a number in functions.php, that will work correctly. But, 'orderby' => 'rand' (just being used for testing purposes) does nothing, the posts are still loaded by date. Any other orderby or other parameter I've tried does not work. If I take the query out of the function and add it directly to the template file, it works fine.

Why is the query in functions.php not recognizing additional parameters? Why does changing posts_per_page or offset work correctly, when other parameters don't?

  • Notes: 1: Use the pagination parameters not the offset parameters ( offsets cause other parameters to change their behaviour and certain features not to work as expected, easier to cache ). 2: There's a utility library that comes with WordPress that would let you do wp.ajax, and automatically registers the admin endpoint. 3: This would be better if you used the WP API 4: You should use pre_get_posts to modify your query, you shouldn't be creating a second new query 5: Use wp_die on your ajax handler not exit 6: Querying post meta is expensive, and slow use taxonomies instead – Tom J Nowell Jun 18 '16 at 13:31
1

For whatever reason, it was an issue specific to the orderby parameter. Adding remove_all_filters('posts_orderby'); to the top of the more_post_ajax() function solved the problem.

  • I don't know if you are using a custom theme, but it appears that there is a filter wrongly used in your theme. Yes, your solution works, but it it does not solve the real issue. Look for the specific filter and make sure that it is properly used and if not, contact the theme developer so that he can rectify it in his next update :-) – Pieter Goosen Jun 4 '15 at 6:47
  • It is a custom theme (based off of a starter theme) but I can't find that filter in use anywhere. Any idea what specifically might be causing it? – Josh Warren Jun 4 '15 at 8:35
  • Unfortunately not. If it is not in your theme, it might be added via a plugin. Well, it has to added by some custom code. I alsways add 'suppress_filters' => true to my custom queries. This way, no custom posts_* filters or pre_get_posts can alter my custom queries. This is also a good indication if your problem is caused by a filter or not. Hope this helps – Pieter Goosen Jun 4 '15 at 8:42
  • 1
    It turns out a plugin was using the filter and causing the issue. I'll start doing what you suggested though, adding 'suppress_filters' => true to queries. Thanks for the help, appreciate it. – Josh Warren Jun 4 '15 at 10:20
  • My pleasure, glad you came to the root of your issue. Enjoy ;-) – Pieter Goosen Jun 4 '15 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.