UPDATE - SOLVED
Okay so I have three custom post types 'courses', 'modules' and 'results'.
These post types are set up with a number of ACF (Advanced Custom Fields).
I'm trying to query the 'results' CPT based on their ACF's using 'meta_query'.
The two ACF fields I'm trying to query with are 'module' and 'user'.
'module' - Set up as a 'Relationship' field type filtering the
'module' custom post type.
'user' - Set up as a (Relational) 'User' field type.
I seem to only get an empty array back. I've looked at the examples on https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field%20parameters still without any luck.
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => $module_id,
'compare' => '=',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
Call function:
print_r(check_results_for_user($post->ID, $currentUserID ));
Result:
Array ( )
UPDATE - Solved:
Okay so managed to figure it out. Was due to the 'module' relationship field saving it’s data as a serialized array: a:1:{i:0;s:1:"9";}
So you have to change the compare to 'LIKE' and wrap the value in "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
See the end of this page: https://www.advancedcustomfields.com/resources/querying-relationship-fields/
UPDATE - Solved:
Okay so managed to figure it out. Was due to the 'module' relationship field saving it’s data as a serialized array: a:1:{i:0;s:1:"9";}
So you have to change the compare to 'LIKE' and wrap the value in "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
See the end of this page: https://www.advancedcustomfields.com/resources/querying-relationship-fields/
Related
I'm having troubles regarding my search. My search query runs fine, it gives the result as it should be, but the problem is it also creates a dummy product of the first searched product.
Like this image:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'property_add_location_property_add_country',
'value' => $_POST['property_add_location_property_add_country'],
'compare' => 'IN',
),
array(
'key' => 'property_add_location_property_add_state',
'value' => $_POST['property_add_location_property_add_state'],
'compare' => 'IN',
),
array(
'key' => 'property_add_location_property_add_city',
'value' => $_POST['property_add_location_property_add_city'],
'compare' => '=',
),
)));
And this is my code which I run on Archie-product.php.
Please help me out, I have been doing everything to solve this for over 4 days but I can't find a solution!
i have users with 2 custom meta fields. Vip (0 or 1) and likes (numeric)
I need to query users with orderby them by Vip and likes, so i need that first was vip users, than users with max likes.
I have such code
$meta_query = array(
'relation' => 'OR',
'vip_clause' => array(
'key'=> 'vip',
'compare' => 'EXISTS' ,
'type' => 'NUMERIC' ,
),
'like_clause' => array(
'key'=> 'al_likes',
'type' => 'NUMERIC',
'compare' => 'EXISTS' ,
),
array(
'key'=> 'vip',
'compare' => 'NOT EXISTS' ,
),
array(
'key'=> 'al_likes',
'compare' => 'NOT EXISTS' ,
),
);
$args = array(
'meta_query' => $meta_query,
'orderby' => array(
'like_clause' => 'DESC',
'vip_clause' => 'DESC'
)
);
$users = new WP_User_Query ( $args );
As a result i get lists of users which are ordered by Vip (them first) but not by likes too. How can i fix it? Thank you
The "orderby" parameter of WP_User_Query doesn't support array value. I guess you took the sample from WP_QUERY, which is for POST data.
Try using simple, comma separated string, instead of array. WP_USER_QUERY can recognize it.
$args = array(
'meta_query' => $meta_query,
'order' => 'DESC,DESC',
'orderby' => 'like_clause,vip_clause',
);
I need some help getting a wp_user_query to work; been struggling and trying lots of stuff but cannot get it to work proper way.
here is my code:
$args = array(
'meta_query' => array(
'role' => 'personal-injury-lawyer',
'orderby' => 'meta_value',
'meta_key' => 'lawyer_numeric_rank',
'order' => 'ASC',
array(
'key' => 'lawyer_location',
'value' => 'London',
'compare' => '='
),
)
);
$london_user_query = new WP_User_Query($args);
if ( ! empty($london_user_query->results)) {
foreach ( $london_user_query->results as $user ) {
I know the foreach loop is not closed; just trying shorten this...
I am trying to do three things on this query:
Show roles of personal-injury-lawyer
Show where meta_key field of lawyer_location = London
Order by meta_value where meta_key = lawyer_numeric_rank in ASC order
I have tried this a number of ways but cannot get this to work with both filtering on London location and also ordering by the rank...
The code right now does filter on location; but the orderby part is not working; if i remove the location filter from this; then the orderby does work...
I hope someone can help with this.
you were forming the meta query argument incorrectly. The below code should help you with quite a few more meta queries as well.
$args = array(
'role'=> 'personal-injury-lawyer', //assuming you have created a role that corresponds..
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'lawyer_numeric_rank',
'meta_query' => array(
// 'relation'=> 'AND', --> if you want more conditions
array(
'key' => 'lawyer_location',
'value' => 'London',
'compare' => '='
),
/* even more conditions with OR, cumulative effect is match lawyer location AND 1 of the nested arrays
array(
'relation' => 'OR',
array(
'key' => '',
'value' => '',
'compare' => '=',
),
array(
'key' => '',
'value' => '',
'compare' => '=',
),
),
*/
)
);
I have the following function which returns only future events, which works great:
$args = array(
'post_type' => self::POST_TYPE,
'posts_per_page' => $posts_per_page,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'status' => 'publish',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)
);
The problem I have is, I also need to check whether a custom field called "post_is_global" has been set (the type is BOOL by the way) but I don't know how to implement it into this query. Any help would be greatly appreciated.
Many thanks!
The query should look somewhat like this:
$args = array(
...
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'post_is_global',
'value' => '1',
'compare' => '=',
),
)
);
$query = new WP_Query($args);
References:
ACF | Query posts by custom fields
Class Reference/WP_Query
I'm attempting to output a list of WordPress users using new WP_User_Query( $args ). The list is made up only of 'Special Members' and 'Superspecial Members':
I am currently using the following 'OR' relation meta_query as my $args to achieve this:
// the Args
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'access',
'value' => 'Special Member',
'compare' => '=',
),
array(
'key' => 'access',
'value' => 'Superspecial Member',
'compare' => '=',
),
)
);
I'd also like to order the outputted list by user last name (as below). Is there some way to add this to the above set of args as an 'And' relation? I have tried appending to the 'Superspecial Member' array, but this breaks the code.
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'last_name',
);
Any ideas?
Don't you want to just do something like this?
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'last_name',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'access',
'value' => 'Special Member',
'compare' => '=',
),
array(
'key' => 'access',
'value' => 'Superspecial Member',
'compare' => '=',
)
)
);