Compare Two meta_values in wordpress - php

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
}

Related

WordPress Query order by two keys in dependency

I sort a query by a customfield start_date (form type Ymd). The sorting should always be descending from new to old. If there are multiple entries for the date on one day, the entries should be sorted by the order of publication.
I tried it with the (post) date, ID, menu_order but it never works in all cases.
The start_date may be a different day than the post date.
Is there any query way?
Or do I have to add hours and minutes to the news_start_date field?
Or maybe switch to the post date over all, but this means in this backend flow a big lost of elegance.
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'news_start_date',
'value' => $today,
'compare' => '<=',
'type' => 'DATE'
),
array(
'relation' => 'OR',
array (
'relation' => 'AND',
array(
'key' => 'news_start_date',
'value' => $expire_date,
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'news_end_date',
'compare' => 'NOT EXISTS'
),
),
array (
'relation' => 'AND',
array(
'key' => 'news_end_date',
'compare' => 'EXISTS'
),
array(
'key' => 'news_end_date',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
),
),
);
$args = array(
'post_type' => 'news',
'meta_query' => $meta_query,
'posts_per_page' => $news_count,
'meta_key' => 'news_start_date',
'orderby' => 'meta_value',
'orderby' => array(
'meta_value' => 'DESC',
'ID' => 'DESC',
//'menu_order' => 'DESC',
//'date' => 'DESC',
//'publish_date' => 'DESC',
),
'order' => 'DESC',
'post__not_in' => $sticky_posts,
'ignore_sticky_posts' => true,
'cache_results' => true,
'update_post_meta_cache' => true,
'no_found_rows' => true,
'suppress_filters' => false,
);
I tried the others fields but there was never a 100% correct order

Wordpress - Multiple Meta Keys - Order by rank

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

Don't display WordPress posts if time has passed using WP_Query

I have a custom post type of schedule which I can add 'events' with a custom field of Timeslot. This custom field is a repeater using ACF Pro.
It contains a date, start time and end time. Date picker and time pickers respectively.
On the homepage I have an image slider that pulls in the artwork and event title and only display 'todays' events, in time order.
What I want to do now is to not show events that have passed. So if the current time is 14:00pm for example, the all shows that ended before that time will not show. If the show started at 13:00pm and ends at 15:00pm though, that show will still show, and will appear first. I hope that makes sense?!
Here is my WP_Query so far but after a few attempts, I cannot figure out how to modify it to work as required:
<?php
$args = array(
'post_type' => 'schedule',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_date',
'compare' => '=',
'value' => $day,
),
'time_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_start_time',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
<?php
$args = array(
'post_type' => 'schedule',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_date',
'compare' => '=',
'value' => $day,
),
'time_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_start_time',
'compare' => '>=',
'value' => time(),
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
Note the change in the time_clause argument. So only show time_clause when it equals or is later than the time now.

Wordpress post query on more than one condition

I have the following function which returns only future events, which works great:
$args = array(
'post_type' => self::POST_TYPE,
'posts_per_page' => $posts_per_page,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'status' => 'publish',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)
);
The problem I have is, I also need to check whether a custom field called "post_is_global" has been set (the type is BOOL by the way) but I don't know how to implement it into this query. Any help would be greatly appreciated.
Many thanks!
The query should look somewhat like this:
$args = array(
...
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'post_is_global',
'value' => '1',
'compare' => '=',
),
)
);
$query = new WP_Query($args);
References:
ACF | Query posts by custom fields
Class Reference/WP_Query

Query by start date and then by start time in Wordpress

I am trying to query the database and ordering the result by the date and then by time. But with the query below it only orders by the start time. When I remove the start time it orders the result by date.
So far I have tried changing the order and multiple solutions throughout the web but nothing works.
$args = array(
'numberposts' => -1,
'post_type' => 'reservation',
'cat' => $categorie,
'meta_key' => 'start_time',
'orderby' => array( 'start_date' => 'ASC', 'meta_value_num' => 'ASC' ),
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $date,
),
array(
'key' => 'reservation_status',
'compare' => '<',
'value' => 3,
)
)
);
// query
$the_query = new WP_Query( $args );
Please help.
Thanks
How about something like this:
order => 'start_date start_time',
orderby => 'ASC',

Categories