Hello I have something like this
if(isset($_POST["select_1"]) or isset($_POST["select_2"])){
$args = array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $meta_key_list1,
'value' => $meta_key_val,
'compare' => '='
),
array(
'key' => $meta_key_list2,
'value' => $meta_key_val,
'compare' => '='
),
array(
'key' => $meta_key_list3,
'value' => $meta_key_val,
'compare' => '='
),
),
);
}else{
$args = array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'page_rank'
);
}
I need show all pages which have set custom field "page_rank" and order it DESC. "Else" working good but first part not working. I try something like in "else" but this not working for multiple key.
Please do you know anyone how to solve this problem?
Thank you !
EDIT:
Solved I changed OR to AND thank you for help
Try 'compare' => 'LIKE' instead of 'compare' => '=' and verify your keys and values in the meta query
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 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
have done a bit of searching round these parts for an answer and yet to find a satisfactory explanation for why my code doesn't work... basically I'm trying to perform a meta_query with 3 arrays and one relational operator. If someone could let me know where I'm going wrong it would be a massive help, bare in mind I have tested this code using only 2 arrays and it works for all the given keys when there are two arrays, just not three:
$args = array(
'post_type' => 'any',
'orderby' => 'post_date',
'posts_per_page' => '6',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'article_feature_on_home_page',
'value' => '1',
'compare' => '='
),
array(
'key' => 'mood_boards_feature_on_home_page',
'value' => '1',
'compare' => '='
),
array(
'key' => 'real_weddings_featured_home_page',
'value' => '1',
'compare' => '='
),
),
);
Any help would be greatly appreciated, cheers!
I feel like im extremely close on this one but the query keeps on showing up empty. Basically, Im trying to query the custom values between two dates. if the start date is less than the current date AND the end date is greater than or equal to the current date
$args = array(
'taxonomy' => 'exhibition_type',
'term' => 'faculty',
'numberposts' => 10,
'post_type' => 'exhibitions',
'meta_key' => 'start_date_of_event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date_of_event',
'value' => $current,
'compare' => '<'
),
array(
'key' => 'end_event_date',
'value' => $current,
'compare' => '>='
)
),
'order_by' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged
);
keeps on turning up 0 results :'( I'm stuck at the moment. Any help would be much appreciated.
its possible. I entered my data in wrong :D
Here is the working code:
$args = array(
'taxonomy' => 'exhibition_type',
'term' => 'faculty',
'numberposts' => 10,
'post_type' => 'exhibitions',
'meta_key' => 'start_date_of_event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date_of_event',
'value' => $current,
'compare' => '<'
),
array(
'key' => 'end_date_of_event',
'value' => $current,
'compare' => '>='
)
),
'order_by' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged
);
This code uses two meta_values (start_date_of_event & end_date_of_event) and compares that to the current date which is ( $current = date('Ymd'); ). Hope this helps others. :D
If I understand you I think this is your answer,
if(strtotime($args['meta_query'][0]['key']) > strtotime($args['meta_query'][1]['key'])){
//do stuff here
}