How to sort multiple wordpress custom field values?

后端 未结 4 1337
被撕碎了的回忆
被撕碎了的回忆 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:05

    Here's an example of using more than one meta_key and orderby that I believe should work:

    $params = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
                'key' => 'price',
                'value' => '',
                'compare' => 'LIKE'
        ),
        array(
                'key' => 'size',
                'value' => '',
                'compare' => 'LIKE'
        )
    ),
    'orderby' => 'price size',
    'order' => 'ASC'
    );
    $query = new WP_Query;
    $resulting_obj = $query->query($params);
    

    You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.

提交回复
热议问题