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

How do I query with multiple custom fields orderby in same column?

programmeradmin1浏览0评论

I have a list of posts, one column of which I am trying to set in alphabetical order. The problem is that these posts have one of two different custom field value types shown in the same column. One of them (customer_name) is a post object and the other (quick_customer_name) is just a regular custom field.

The code I have tried below gets me halfway there, it does query the posts with one of those values at the top but still not in alphabetical order.

$posts_per_page = 10;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'offerter',
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'meta_key' => 'business_id',
    'meta_value' => $business_id,
    'meta_query' => array(
        'relation' => 'OR',
        'customer_name' => array(
            'key' => 'customer_name',
            'compare' => 'LIKE',
        ),
        'quick_customer_name' => array(
            'key' => 'quick_customer_name',
            'compare' => 'LIKE',
        ),
    ),
    'orderby' => array(
        'customer_name' => 'desc',
        'quick_customer_name' => 'desc',
    )

I have a list of posts, one column of which I am trying to set in alphabetical order. The problem is that these posts have one of two different custom field value types shown in the same column. One of them (customer_name) is a post object and the other (quick_customer_name) is just a regular custom field.

The code I have tried below gets me halfway there, it does query the posts with one of those values at the top but still not in alphabetical order.

$posts_per_page = 10;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'offerter',
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'meta_key' => 'business_id',
    'meta_value' => $business_id,
    'meta_query' => array(
        'relation' => 'OR',
        'customer_name' => array(
            'key' => 'customer_name',
            'compare' => 'LIKE',
        ),
        'quick_customer_name' => array(
            'key' => 'quick_customer_name',
            'compare' => 'LIKE',
        ),
    ),
    'orderby' => array(
        'customer_name' => 'desc',
        'quick_customer_name' => 'desc',
    )
Share Improve this question asked Jul 9, 2019 at 11:22 ViktorViktor 31 bronze badge 5
  • If customer_name is a post object (from ACF, I assume?), then you're not going to be able to sort by it with WP_Query, because all that's stored in meta is the post ID. – Jacob Peattie Commented Jul 9, 2019 at 12:09
  • @JacobPeattie Yes is a post object from ACF. It worked just fine when it was set to 'orderby' => 'customer_name', before i added meta_query and the second regular custom field quick_customer_name – Viktor Commented Jul 9, 2019 at 12:30
  • It would have been a coincidence. All that's stored with a post object field is the post ID, so that's all you can sort by for that field. – Jacob Peattie Commented Jul 9, 2019 at 12:31
  • Aah I see, thanks for letting me know! – Viktor Commented Jul 9, 2019 at 12:59
  • Solved it by creating a 3rd custom field and a function to save either the ´customer_name´ or ´quick_customer_name´ to the new field. Then do a orderby on that third field. – Viktor Commented Jul 9, 2019 at 13:28
Add a comment  | 

1 Answer 1

Reset to default 0

Solved it by creating a 3rd custom field and a function to save either the ´customer_name´ or ´quick_customer_name´ to the new field. Then do a orderby on that third field.

发布评论

评论列表(0)

  1. 暂无评论