plugins – Compare custom field values

Question

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' => 'AND',
                        array('key' => 'Time:',
                        'value' => 'hour',
                        '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%' AND time like '%1%' 

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, 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.

0
Wordprez 1 year 2022-02-17T06:31:11-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse