一件不可思议的事情呈现在我面前:
使用post和ajax方法创建的自定义过滤器可以工作。
相反,LoopMulk按钮加载我的自定义帖子,但不考虑过滤器。
我相信wpquery调用有一些异常,$wp\u query->query\u vars似乎无法正常工作。就好像这两个功能不相互通信一样。
script.js
jQuery(function($){
/*
* Load More
*/
$('#misha_loadmore').click(function(){
$.ajax({
url : misha_loadmore_params.ajaxurl, // AJAX handler
data : {
'action': 'loadmore', // the parameter for admin-ajax.php
'query': misha_loadmore_params.posts, // loop parameters passed by wp_localize_script()
'page' : misha_loadmore_params.current_page // current page
},
type : 'POST',
beforeSend : function ( xhr ) {
$('#misha_loadmore').text('Loading...'); // some type of preloader
},
success : function( data ){
if( data ) {
$('#misha_loadmore').text( 'More posts' );
$('#misha_posts_wrap').append(data); // insert new posts
misha_loadmore_params.current_page++;
if ( misha_loadmore_params.current_page == misha_loadmore_params.max_page )
$('#misha_loadmore').hide(); // if last page, HIDE the button
} else {
$('#misha_loadmore').hide(); // if no data, HIDE the button as well
}
}
});
return false;
});
/*
* Filter*/
$('#misha_filters').submit(function(){
$.ajax({
url : misha_loadmore_params.ajaxurl,
data : $('#misha_filters').serialize(), // form data
dataType : 'json', // this data type allows us to receive objects from the server
type : 'POST',
beforeSend : function(xhr){
$('#misha_filters').find('button').text('Filtering...');
},
success:function(data){
// when filter applied:
// set the current page to 1
misha_loadmore_params.current_page = 1;
// set the new query parameters
misha_loadmore_params.posts = data.posts;
// set the new max page parameter
misha_loadmore_params.max_page = data.max_page;
// change the button label back
$('#misha_filters').find('button').text('CERCA');
// insert the posts to the container
$('#misha_posts_wrap').html(data.content);
// hide load more button, if there are not enough posts for the second page
if ( data.max_page < 2 ) {
$('#misha_loadmore').hide();
} else {
$('#misha_loadmore').show();
}
}
});
// do not submit the form
return false;
});
});
functions.php
function misha_script_and_styles() {
// absolutely need it, because we will get $wp_query->query_vars and $wp_query->max_num_pages from it.
global $wp_query;
// when you use wp_localize_script(), do not enqueue the target script immediately
wp_register_script( 'misha_scripts', get_stylesheet_directory_uri() . '/js/script.js', array('jquery') );
// passing parameters here
wp_localize_script( 'misha_scripts', 'misha_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
'current_page' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
'max_page' => $wp_query->max_num_pages
) );
wp_enqueue_script( 'misha_scripts' );
}
// LOAD More
// LOAD MORE NEW
add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}
function misha_loadmore_ajax_handler(){
// prepare our arguments for the query
$params = json_decode( stripslashes( $_POST['query_vars'] ), true );
$params['paged'] = $_POST['page'] + 1; // we need next page to be loaded
$params['post_type'] = 'cars';
$params['post_status'] = 'publish';
$params['orderby'] = 'date'; // add this to order by date
$params['order'] = 'DESC'; // add this to display the most recent
// it is always better to use WP_Query but not here
query_posts( $params );
//$query = new WP_Query($params);
//global $wp_query;
if(have_posts() ) :
// run the loop
while(have_posts() ): the_post();
include ('page-templates/content/post-loadmore.php');
endwhile;
endif;
die; // here we exit the script and even no wp_reset_query() required!
}
// Filter
add_action('wp_ajax_mishafilter', 'misha_filter_function');
add_action('wp_ajax_nopriv_mishafilter', 'misha_filter_function');
function misha_filter_function(){
$params = array(
'posts_per_page' => $_POST['10'],
'post_type' => 'cars',
'orderby' => 'date', // we will sort posts by date
'status' => 'published',
'order' => $_POST['date'], // ASC or DESC
'tax_query' => [],
'meta_query' => [
'relation' => 'AND',
],
);
if( isset($_POST['code']) )
{
if(!empty($_POST['code']))
{
$params['meta_query'][] = array(
'key' => 'codice_cars',
'value' => sanitize_text_field( $_POST['code']) ,
);
}
}
if( isset($_POST['tipologia_cars']) )
{
if(!empty($_POST['tipologia_cars']))
{
$params['tax_query'][] = [
'taxonomy' => 'tipologia_cars',
'field' => 'slug',
'terms' => array( sanitize_text_field( $_POST['tipologia_cars'] ) )];
}
}
if( isset($_POST['comune']) )
{
if(!empty($_POST['comune']))
{
$params['tax_query'][] = [
'taxonomy' => 'comune',
'field' => 'slug',
'terms' => array( sanitize_text_field( $_POST['comune'] ) )];
}
}
if( isset($_POST['price_above']) )
{
if(!empty($_POST['price_above']))
{
if( $_POST['price_above'] == 50000 ) {
$params['meta_query'][] = array(
'key' => 'prezzo',
);
} else {
$params['meta_query'][] = array(
'key' => 'prezzo',
'value' => sanitize_text_field( $_POST['price_above']) ,
'type' => 'numeric',
'compare' => '>='
);
}
}
}
if( isset($_POST['price_below']) )
{
if(!empty($_POST['price_below']))
{
if( $_POST['price_below'] == 2000000 ) {
$params['meta_query'][] = array(
'key' => 'prezzo',
);
} else {
$params['meta_query'][] = array(
'key' => 'prezzo',
'value' => sanitize_text_field( $_POST['price_below']) ,
'type' => 'numeric',
'compare' => '<='
);
}
}
}
if( isset($_POST['sortposts']) )
{
// let's create new array with args
if( !empty( $_POST['sortposts'] ) ) {
// I recommend to explode it because we're take 2 param values from one $_GET variable
$sort_params = explode( "-", $_POST['sortposts'] );
// $sort_args[0] is what we sort
// $sort_args[1] is how we sort - Ascending or Descending
$params['order'] = $sort_params[1];
if( $sort_params[0] == 'date' ) {
// date is a default WP_Query value, so we do nothing
$params['orderby'] == 'date';
} elseif( $sort_params[0] == 'property_price' ) {
$params['orderby'] = 'meta_value_num';
$params['meta_key'] = 'prezzo';
}
}
}
query_posts( $params );
global $wp_query;
if( have_posts() ) :
ob_start(); // start buffering because we do not need to print the posts now
while( have_posts() ): the_post();
include ('page-templates/content/post-loadmore.php');
endwhile;
$content = ob_get_contents(); // we pass the posts to variable
ob_end_clean(); // clear the buffer
endif;
// no wp_reset_query() required
echo json_encode( array(
'posts' => json_encode( $wp_query->query_vars ),
'max_page' => $wp_query->max_num_pages,
'found_posts' => $wp_query->found_posts,
'content' => $content
) );
die();
}
暂无答案!
目前还没有任何答案,快来回答吧!