I have created a page that receives information and filters the posts by that choice. The page gets the information with _GET and I use the following code to send the query
$postss = new WP_Query([
'post_type' => 'post',
'meta_query' =>
array(
array(
'key' => 'City:',
'value' => $city,
'compare' => 'LIKE',
),
array(
'relation' => 'OR',
array('key' => 'Time:',
'value' => 'hour',
'compare' => 'LIKE',
),
array('key' => 'Time:',
'value' => 'min',
'compare' => 'LIKE',
),
),
),
'cat' => $category,
'posts_per_page' => 9,
'post_status' => 'publish',
'paged' => $paged
]);
However my issue is, I have an input where the user can specify the city, then the category and then the time. The city and category work fine, but when we come to time I have placed few options with a drop down : under 1 hour, under 2 hours, under 3 hours etc . With this I can easily check with php if the time value is 1 2 3 etc and simply create the query
where time LIKE '%hour%'
however when the user chooses the 1, means that it's not just 1 hour, but it can also include minutes, so for example 30 minutes, so the results shold include 1 hour, but all minutes, so the query should be something like
where time like '%min%' OR time like '%hour%'
Also there is an issue, I don't know how exactly to add the % symbol (because sometimes there is 10 min sometimes it's 20 minutes - plural) so
LIKE '%min%'
would be okay, but not the same for hous, since if i do
LIKE '%hour%'
it will also include hours (2 3, not just 1 hour), so it should be
where time like '%hour'
in sql is easy, but with the wp_query I am not sure where to place the % symbol.
ps:I read that when I use the LIKE with meta_query automatically adds the % symbols at fron and end of the string, is there a way to remove them and just add it at front or in back, but not both?
Example values in meta: City:New York, Tokyo, London
Time:10 minutes, 5 minutes, 20 minutes, 1 hour, 1.5 hours, 2 hours, 3 hours
I have created a page that receives information and filters the posts by that choice. The page gets the information with _GET and I use the following code to send the query
$postss = new WP_Query([
'post_type' => 'post',
'meta_query' =>
array(
array(
'key' => 'City:',
'value' => $city,
'compare' => 'LIKE',
),
array(
'relation' => 'OR',
array('key' => 'Time:',
'value' => 'hour',
'compare' => 'LIKE',
),
array('key' => 'Time:',
'value' => 'min',
'compare' => 'LIKE',
),
),
),
'cat' => $category,
'posts_per_page' => 9,
'post_status' => 'publish',
'paged' => $paged
]);
However my issue is, I have an input where the user can specify the city, then the category and then the time. The city and category work fine, but when we come to time I have placed few options with a drop down : under 1 hour, under 2 hours, under 3 hours etc . With this I can easily check with php if the time value is 1 2 3 etc and simply create the query
where time LIKE '%hour%'
however when the user chooses the 1, means that it's not just 1 hour, but it can also include minutes, so for example 30 minutes, so the results shold include 1 hour, but all minutes, so the query should be something like
where time like '%min%' OR time like '%hour%'
Also there is an issue, I don't know how exactly to add the % symbol (because sometimes there is 10 min sometimes it's 20 minutes - plural) so
LIKE '%min%'
would be okay, but not the same for hous, since if i do
LIKE '%hour%'
it will also include hours (2 3, not just 1 hour), so it should be
where time like '%hour'
in sql is easy, but with the wp_query I am not sure where to place the % symbol.
ps:I read that when I use the LIKE with meta_query automatically adds the % symbols at fron and end of the string, is there a way to remove them and just add it at front or in back, but not both?
Example values in meta: City:New York, Tokyo, London
Time:10 minutes, 5 minutes, 20 minutes, 1 hour, 1.5 hours, 2 hours, 3 hours
Share Improve this question edited Feb 17, 2022 at 13:52 Wordprez asked Feb 17, 2022 at 11:31 WordprezWordprez 271 silver badge8 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 0Instead of storing your values as "5 hours 13 minutes" instead store them as timestamps, e.g. 5:13
for 5h 13m
This way you can use the date/time comparator operations to answer questions
E.g. to get items under 5 hours and 30 minutes:
$args = [
...
'meta_query' => [
[
'meta_key' => 'time',
'meta_value' => date( "5:30" ),
'meta_compare' => '<',
'type' => 'TIME',
],
],
];
$query = new WP_Query( $args );
This will match a 3 hour post, or a 4:20 post, or a 5h 29min post.
You may need to add :00
for the seconds on to the end of all code and values.
For display you can use human_readable_duration
. This will include the seconds though, but the function is very simple so copying and modifying it to your requirements should be straight forward.
city
taxonomy instead of using post meta. – Tom J Nowell ♦ Commented Feb 17, 2022 at 12:19YYYY-MM-DD HH:MM:SS
, but even storing it as just the number of minutes e.g.60
would let you use the math comparators such asBETWEEN
or>
. You can use WP functions such ashuman_time_diff
orhuman_readable_duration
to display it as human readable text, and it'll even localise it. Your current solution is unintelligable in say german or japanese – Tom J Nowell ♦ Commented Feb 17, 2022 at 17:06