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?
1 Answer
Reset to default 1For 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.ajax
, and automatically registers the admin endpoint. 3: This would be better if you used the WP API 4: You should usepre_get_posts
to modify your query, you shouldn't be creating a second new query 5: Usewp_die
on your ajax handler notexit
6: Querying post meta is expensive, and slow use taxonomies instead – Tom J Nowell ♦ Commented Jun 18, 2016 at 13:31