Home › Forums › Backend Issues (wp-admin) › Query two fields and orderby another
I’m building a directory with a ‘dmu_profile” custom post type. I want to be able to create a query that selects ‘Faculty’ and ‘Staff’ and then orders them by their ‘last_name’.
I have a checkbox that is called ‘profile_type’ that has many options including ‘Faculty’ and ‘Staff’ and a textfield called ‘last_name’.
Here is the query I’m trying:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'dmu_profile',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'suppress_filters' => false,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'profile_type',
'value' => 'Faculty',
'compare' => 'LIKE'
),
array(
'key' => 'profile_type',
'value' => 'Staff',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
This results in my server hanging. What is weird is that if I remove one of the meta_query options it works:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'dmu_profile',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'suppress_filters' => false,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'profile_type',
'value' => 'Faculty',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
Or if I remove the ‘orderby’ the rest of the query works:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'dmu_profile',
'suppress_filters' => false,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'profile_type',
'value' => 'Faculty',
'compare' => 'LIKE'
),
array(
'key' => 'profile_type',
'value' => 'Staff',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
Any ideas?
I found a WP ticket that says there’s currently an issue with meta_query and the ‘OR’ relation that I think is causing the issue – https://core.trac.wordpress.org/ticket/25538
As a temporary solution I’m instead filtering out all non ‘Faculty’ and ‘Staff’ members by using ‘NOT LIKE’ meta_query(s):
'meta_query' => array(
array(
'key' => 'profile_type',
'value' => 'Adjunct',
'compare' => 'NOT LIKE'
),
array(
'key' => 'profile_type',
'value' => 'Emeriti',
'compare' => 'NOT LIKE'
),
(etc...)
)
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.