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!
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!
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
I'm trying to use a custom WP_Query but the results are not right.
I want to do is create a custom form to filter the Jobs registered with "WP Job Manager" using radius search.
This is my query:
$args = array(
'post_type' => 'job_listing',
'post_status' => array( 'publish' ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'geolocation_long',
'value' => array($maxLong,$minLong),
'compare' => 'between',
'type' => 'numeric',
),
array(
'key' => 'geolocation_lat',
'value' => array($maxLat,$minLat),
'compare' => 'between',
'type' => 'numeric',
),
),
);
$the_query = new WP_Query( $args );
Can you spot any issue?
Any help would be appreciated. Thank you!
This may be a problem with the compare BETWEEN feature. I would suggest you have a look at the exact query being generated and whether the compare is actually comparing floats, not strings (which might produce unexpected results).
Have a look at the troubleshooting done for a very similar query over on wordpress.stackexchange.com.
The following is quoted from the question:
Since postmeta values are stored as strings, I figured I should be casting to DECIMAL, but it just seems to trim the decimal value from the string due to the lack of DECIMAL arguments/precision parameters.
and review Rarst's answer:
You can filter generated SQL and add precision parameters that you need.
Enable filters for get_posts() by adding following to query:
'suppress_filters' => false,
And:
add_filter('get_meta_sql','cast_decimal_precision');
function cast_decimal_precision( $array ) {
$array['where'] = str_replace('DECIMAL','DECIMAL(10,3)',$array['where']);
return $array;
}
Notice OP's comment on this suggestion too though:
tried both solutions and it looks like it compares them as strings with single quotes (wp 3.1.1), any ideas on that? trying raw sql did work though.
Please update your question with further findings to enable us to help you out.
change $minlong and $maxlong
$args = array(
'post_type' => 'job_listing',
'post_status' => array( 'publish' ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'geolocation_long',
'value' => array($minLong,$maxLong),
'compare' => 'between',
'type' => 'numeric',
),
array(
'key' => 'geolocation_lat',
'value' => array($minLat,$maxLat),
'compare' => 'between',
'type' => 'numeric',
),
),
);
$the_query = new WP_Query( $args );
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 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
}