在自定义 WordPress 查询循环中按多个参数排序

Ordering by multiple parameters in a custom WordPress query loop

我实现了无限滚动,但在按价格或任何自定义值排序的搜索结果中它不起作用。 在我排队的脚本中:-

 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] 吗?如果我不应该将我所有的帖子 ID 传递给 ajax 处理程序对它们进行迭代并对它们进行排序(如果是这种情况,如何处理我的自定义 order_by 参数)?

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

因此,使用 WordPress 的 OrderBy,您有几个不同的选择。

如果您希望两个参数都按相同的 ASC 或 DESC 方向排序,则参数需要一个字符串,参数之间用 space.

分隔

Multiple 'orderby' values Display pages ordered by 'title' and 'menu_order'. (title is dominant):

$args = array(
  'post_type' => 'page',
  'orderby'   => 'title menu_order',
  'order'     => 'ASC',
);
$query = new WP_Query( $args );

当您对每个参数进行不同排序时,您使用了一个数组:

Multiple 'orderby' values using an array

> 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']);
          }