How to ignore search string case when using WP_User_Query? - php

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'
);

Related

Wordpress meta_query >= & <= operator only retrieving posts equal to numeric value

I have WordPress query arguments that need to filter posts based on two meta key's values based on if the two values are >= and <= to two numbers.
$args = array(
'post_type' => 'vacancy',
'post_status' => 'publish'
);
$vacancy_small_salary = 3000; //these are dynamically inputted from $_POST
$vacancy_large_salary = 5000; //these are dynamically inputted from $_POST
//append search queries to arguments
if ( !is_null($vacancy_small_salary) && !is_null($vacancy_large_salary) )
$args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => 'vacancy_small_salary',
'value' => $vacancy_small_salary,
'operator' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'vacancy_large_salary',
'value' => $vacancy_large_salary,
'operator' => '<=',
'type' => 'NUMERIC'
)
);
The problem I'm getting is that it won't find posts that have a $vacancy_small_salary greater than the inputted var, and $vacnacy_large_salary smaller than it's inputted var.
It does however find posts when the two values equal each other (i.e. if a post has 3000 as the small salary and 5000 as the large salary.)
Could it be that my logic is wrong with the way it manages >= and <=, or maybe the values aren't truly numeric so it cannot search properly? <- I suspect this is not the case, but it's been racking my head for a while now.
As always if you require any more information please ask. Thank you.
Looks like I found the solution.
I needed to use compare instead of operator in the meta_query
so
array(
'key' => 'vacancy_small_salary',
'value' => $vacancy_small_salary,
'operator' => '>=',
'type' => 'NUMERIC'
),
becomes
array(
'key' => 'vacancy_small_salary',
'value' => $vacancy_small_salary,
'compare' => '>=',
'type' => 'NUMERIC'
),
and the same for the other query

looping inside an array using for each

I'm working on a wp-query using php.
first I need to get an array of posts ID, using this
$artists_ID_array = get_field("page_artiste");
(I'm using Advanced Custom Fields with relationship, anyway it doesn't matter... I get an array of values)
here is what the array looks like :
Array ( [0] => 141 [1] => 59 )
then comes my args :
$videos = get_posts(
array(
'post_type' => 'videos',
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[0],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[1],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[2],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[3],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[4],
'compare' => 'LIKE'
and so on... in my meta query, I'm using all the arrays results.
It works fine, I'm getting the posts I want.
But as you can imagine what I'm trying to do is to use for each to avoid having all thoses arrays inside my args "$artists_ID_array[4]", and looping inside my array.
so here is what I've tried, but it's not working... and I can't understand why...
<?php
$artists_ID_array = get_field("page_artiste");
$videos = array(
'post_type' => 'videos',
'post__not_in' => array( $post->ID ),
'meta_query' => array(
)
);
foreach($artists_ID_array as $value) {
array_push($videos['meta_query'], array(
'key' => 'page_artiste',
'value' => $value,
'compare' => 'LIKE'
));
}
?>
can anybody help me with this ?
hope you understand my request
I believe you can simplify this even further, since the meta_query compare allows you to use the value "IN"
"IN" (instead of like) allows you to search the values for any of the values in a given array.
So your get_posts could look something like this:
$videos = get_posts(
array(
'post_type' => 'videos',
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'page_artiste',
'value' => $artists_ID_array,
'compare' => 'IN'
)
)
)
);

Custom WP_Query not working as intended

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 );

Need assistance with wp_user_query not working

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' => '=',
),
),
*/
)
);

Custom field query multiple post types - Wordpress

I m trying to query two custom post types for data and the below isn't working. I tried a few versions ... but no luck.
$args = array(
'post_type' => array('custom1', 'custom2'),
'meta_query' => array(
'relation' => 'AND',
//the first two keys are custom fields found in custom1 custom posts
array(
'key' => 'firstKey',
'value' => 3
),
array(
'key' => 'secondKey',
'value' => cool
),
//the third key can be found in custom2 custom posts
array(
'key' => 'thirdKey',
'value' => 3
)
)
);
Based on your comments: If you want to use custom queries for different types of users, then you probably want to create your query dynamically.
For example, if users of type 'cool' get special treatment, then the basic idea would be:
$args = array( 'post_type' => 'custom' );
if (user is 'cool') {
$args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => 'secondKey',
'value' => 'cool'
),
array(
'key' => 'thirdKey',
'value' => 3
)
};
} else {
$args['meta-key'] = 'thirdKey';
$args['meta_value'] = 3
}
This just pass this as WP_Query($args) like you would if it had been static.

Categories