我已经实现了无限滚动,并且在按价格或任何自定义值进行订购时无法搜索到搜索结果。 在我排队的脚本中:-
isset($_GET['orderby'])?$ga_order_by = $_GET['orderby']: $ga_order_by = '';//grabbing the orderby value
if( gettype($result) == 'object') {
$ga_wp_query = new \WP_Query([ 'post_type'=> ['product_variation', 'product'], 'post__in' => $includes, 'orderby' => ['post__in',$ga_order_by], 'order' => 'ASC' ]);//so i'm ordering by search results and dynamically grabbed value.
} else {
$ga_wp_query = new \WP_Query([ 'post_type'=> 'product', 'post__in' => $includes, 'orderby' => ['post__in',$ga_order_by], 'order' => 'ASC']);
}
$args['ga_search_posts'] = json_encode($ga_wp_query->query_vars);
在我的Ajax处理函数内部进行搜索时调用:-
$search_query = json_decode( stripslashes( $_POST['search_posts'] ), true );//this is the $args['ga_search_posts'] i'm posting via my javascript
$search_query['post_status'] = 'publish';
$search_query['posts_per_page'] = get_option('posts_per_page');
$search_query['paged'] = $_POST['page'] + 1;
wc_set_loop_prop( 'total', $_POST['search_count'] );
add_filter( 'woocommerce_get_price_html', 'labtag_show_price' );
ob_start();
query_posts( $search_query);
if ( have_posts() ) {//product loop
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
wc_get_template_part( 'content', 'product' );
}
}
}
$data = ob_get_clean();
die($data);
exit;
这有效,除非我尝试按任何参数(例如价格等)进行订购。'orderby'=> ['post__in',$ ga_order_by]不能像数组一样声明吗? ajax处理程序对它们进行迭代并对其进行排序(如果是这种情况,如何处理我的自定义order_by params)?
答案 0 :(得分:0)
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
因此,使用WordPress的OrderBy,您有两种不同的选择。
如果您希望两个参数都按ASC或DESC的相同方向排序,则该参数需要一个字符串,且参数之间用空格分隔。
多个“ orderby”值显示按“标题”和“ 'menu_order'。 (标题占主导):
$args = array( 'post_type' => 'page', 'orderby' => 'title menu_order', 'order' => 'ASC', ); $query = new WP_Query( $args );
在对每个参数进行不同排序时使用数组:
使用数组的多个“ orderby”值
> Display pages ordered by 'title' and 'menu_order' with different sort > orders (ASC/DESC) (available since Version 4.0): $args = array( 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) ); $query = new WP_Query( $args );
在您的情况下,由于您使用的是变量,请考虑构建字符串,然后在参数数组中使用它,即:
//start with a space, then .= to concatenate the $_GET parameter with the space if it's set, or clear the string if it's not.
$ga_order_by = " ";
isset($_GET['orderby'])?$ga_order_by .= $_GET['orderby']: $ga_order_by = '';
//grabbing the orderby value and building our complete string.
$orderBy = 'post__in'.$ga_order_by;
if (gettype($result) == 'object') {
$ga_wp_query = new \WP_Query([ 'post_type'=> ['product_variation', 'product'], 'post__in' => $includes, 'orderby' => $orderBy , 'order' => 'ASC' ]);//so i'm ordering by search results and dynamically grabbed value.
} else {
$ga_wp_query = new \WP_Query([ 'post_type'=> 'product', 'post__in' => $includes, 'orderby' => $orderBy, 'order' => 'ASC']);
}