最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to sort a WP_Query by a custom field AND ALSO filter by a different custom field

programmeradmin4浏览0评论

I have a WP_Query call set to load a custom post type that I've created. I want to be able to alpha-sort by one of the custom fields in the posts (MY_NAME_FIELD in the below code) while ALSO filtering posts that have a specific flag on a different custom field (SHOW_POST in the below code).

Here's what I've tried:

$query_args = array(
  'post_type' => 'MY_POSTTYPE_NAME',
  'post_per_page' => -1,
  'meta_key' => 'SHOW_POST',
  'meta_value' => '1',
  'orderby' => 'MY_NAME_FIELD',
  'order' => 'ASC',
);
$query = new WP_Query( $query_args );

But the above code doesn't seem to work, it's still sorting by entry date. If I remove the orderby and order lines, the filtering part works as expected.

I've found several posts on StackOverflow that show how filtering by multiple custom fields works, but haven't been able to find anything where you're filtering by one custom field and ordering by another, and this is what I need to do.

Thank you in advance for any assistance you can provide!

I have a WP_Query call set to load a custom post type that I've created. I want to be able to alpha-sort by one of the custom fields in the posts (MY_NAME_FIELD in the below code) while ALSO filtering posts that have a specific flag on a different custom field (SHOW_POST in the below code).

Here's what I've tried:

$query_args = array(
  'post_type' => 'MY_POSTTYPE_NAME',
  'post_per_page' => -1,
  'meta_key' => 'SHOW_POST',
  'meta_value' => '1',
  'orderby' => 'MY_NAME_FIELD',
  'order' => 'ASC',
);
$query = new WP_Query( $query_args );

But the above code doesn't seem to work, it's still sorting by entry date. If I remove the orderby and order lines, the filtering part works as expected.

I've found several posts on StackOverflow that show how filtering by multiple custom fields works, but haven't been able to find anything where you're filtering by one custom field and ordering by another, and this is what I need to do.

Thank you in advance for any assistance you can provide!

Share Improve this question asked Jan 30, 2020 at 22:09 da7idda7id 31 silver badge2 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

to sort the posts by a meta field you need to have this field in meta_query and in orderby like this

$query_args = array(
  'post_type' => 'MY_POSTTYPE_NAME',
  'post_per_page' => -1,
  'meta_query' => array( 
      'show_post_query' => array(
          'key' => 'SHOW_POST',
          'value' => '1'
      ),
      'MY_FIELD_NAME__order_by' => array(
          'key' => 'MY_FIELD_NAME',
          'type' => 'NUMERIC',
          'compare' => 'NUMERIC',
      )
  ),
  'orderby' => array( 'MY_FIELD_NAME__order_by' => 'ASC' ),
);
$query = new WP_Query( $query_args );

Note the MY_FIELD_NAME__order_by does not have "value" set so it will not be used for searching but will be (let's say) named so you can use it as column name when sorting.

发布评论

评论列表(0)

  1. 暂无评论