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
|
1 Answer
Reset to default 0Solved 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335323a4623074.html
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'orderby' => 'customer_name',
before i added meta_query and the second regular custom fieldquick_customer_name
– Viktor Commented Jul 9, 2019 at 12:30