Home › Forums › Backend Issues (wp-admin) › Filtering on custom field and sorting on another custom field
This support topic shows how to Order by a custom field:
https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
This support topic shows how to filter on 1 or more custom fields:
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
But how do I do both?
Part one – I want to filter on the value from the radio button field ‘member_category’ and limit it to the value ‘Leadership’. THIS WORKS
<?php
$args = array (
'numberposts' => -1,
'post_type' => 'member',
'post_status' => 'publish',
'meta_key' => 'member_category',
'meta_value' => 'Leadership',
//'meta_key' => 'name_last_name',
//'orderby' => 'meta_value',
'order' => 'ASC'
);
?>
I want to sort on the sub field ‘last_name’ which is is a group container of ‘name’. – THIS WORKS
<?php
$args = array (
'numberposts' => -1,
'post_type' => 'member',
'post_status' => 'publish',
//'meta_key' => 'member_category',
//'meta_value' => 'Leadership',
'meta_key' => 'name_last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
?>
It tried combining them, but this DOES NOT WORK.
<?php
$args = array (
'numberposts' => -1,
'post_type' => 'member',
'post_status' => 'publish',
//'meta_key' => 'member_category',
//'meta_value' => 'Leadership',
//'meta_key' => 'name_last_name',
//'orderby' => 'meta_value',
//'order' => 'ASC',
'meta_query' => array (
array (
'meta_key' => 'name_last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
),
array (
'meta_key' => 'member_category',
'meta_value' => 'Leadership'
)
)
);
?>
Any clues?
HI @ger-ang
I think you can do something like:
<?php
$args = array (
'numberposts' => -1,
'post_type' => 'member',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
'leadership_clause' => array(
'key' => 'member_category',
'value' => 'Leadership',
),
'name_clause' => array(
'key' => 'name_last_name',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'leadership_clause' => 'ASC',
'name_clause' => 'ASC',
),
);
Code is untested!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.