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 );
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 am trying to run a WP_Query in WordPress to retrieve posts dated after today using a custom field. My key _mcd_event_date_end has values stored in the m/d/Y format (02/16/2016).
$args = array(
'order' => 'DESC',
'posts_per_page' => -1,
'post_type' => 'mcdevent'
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_mcd_event_date_end',
'value' => date("m/d/Y"),
'compare' => '>=',
),
array(
'key' => '_mcd_event_type',
'value' => 'Other Event',
'compare' => '=',
),
),
);
$other_events = new WP_Query($args);
wp_reset_postdata();
This is the code I am running, and it works for dates posted this year, but not prior. Meaning if I have a post with the _mcd_event_date_end key having a value of something like 4/16/2016 it will show up and if it is something like 2/12/2016 it will not show up. But then anything from 12/31/2015 and before also shows up.
Thanks for your help and please let me know if there's any more information I can provide.
You should be using "date" for the custom field type:
array(
'key' => '_mcd_event_date_end',
'value' => date('Y-m-d H:i:s'),
'compare' => '>=',
'type' => 'DATE'
),
But I think the data must be in "YYYY-MM-DD" format for it to work.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
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' => '=',
),
),
*/
)
);
Using the following code, how can I do the search by ignoring the case of the field in the database :
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
array(
'key' => 'province',
'value' => 'kzn',
'compare' => '='
),
)
)
);
$user_query = new WP_User_Query( $args );
In the above example, 'kzn' is stored as KZN in the database. The input search string can be 'Kzn', or 'kZn... etc. The value to search for can also be a mixed bag as far as it's case goes. So I guess what I am looking for is a war to search for uppercase(databasefield) in the value field, if that makes sense.
Thank you.
Got it.
(
'key' => 'province',
'value' => ('^'.$province),
'compare' => 'REGEXP'
);
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!