排序方式在自定义WordPress的查询循环多个参数

问题描述 投票:0回答:1

我实现了一个无限滚动和搜索按价格或不工作的任何自定义值后排​​序。在这里我排队的脚本中: -

 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; 

这工作,除非我尝试任何参数命令说价格等无法“排序依据” => [“post__in”,$ ga_order_by]声明如下数组?如果不是我应该通过我的所有帖子的ID将AJAX处理程序迭代他们,对它们进行排序(如果是这样的话,如何处理我的自定义ORDER_BY PARAMS)?

php wordpress woocommerce infinite-scroll
1个回答
0
投票

https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

所以,用WordPress的排序依据,你有几个不同的选择。

如果你想这两个参数在ASC或DESC的同一方向进行排序,然后论证预计一个字符串,用空格隔开的参数。

多个“排序依据”值由“标题”和“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']);
          }
© www.soinside.com 2019 - 2024. All rights reserved.