How to sort multiple wordpress custom field values?

后端 未结 4 1280
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 12:47

Display posts with \'Product\' type ordered by \'Price\' custom field:

$query = new WP_Query( 
                      array ( \'post_type\' => \'product\', 
          


        
4条回答
  •  终归单人心
    2021-02-06 13:13

    Thanks to Bainternet I found the solution:

    function orderbyreplace($orderby) {
        return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
    }
    

    and...

    $args = array(
      'post_type'=>'Events',
      'orderby' => 'menu_order',
      'order' => 'ASC',
      'meta_query' => array(
            array(
                'key' => 'Start_Hour',
                'value' => '',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'Start_Minute',
                'value' => '',
                'compare' => 'LIKE'
            )
        )
    );
    
    add_filter('posts_orderby','orderbyreplace');
    $loop = new WP_Query( $args );
    remove_filter('posts_orderby','orderbyreplace');
    

提交回复
热议问题