wp query - Ajax load more posts - WP_Query parameters not working

I'm trying to replace the standard WP pagination with an Ajax load more posts button. I'm basically following

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?

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?

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked Jun 4, 2015 at 5:35 Josh WarrenJosh Warren 1892 silver badges9 bronze badges 1
  • 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 Commented Jun 18, 2016 at 13:31
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745135032a4613159.html

相关推荐

  • wp query - Ajax load more posts - WP_Query parameters not working

    I'm trying to replace the standard WP pagination with an Ajax load more posts button. I'm basically following

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信