WordPress Meta-Query between 2 dates - php

I have currently a small problem understanding with meta-queries by WordPress. Initial situation:
A Custom Post Type with 2 meta-Fields (offer_start-date, offer_end-date) The CPT is intended as an offer, which should be displayed in the specified time period (between start-date and end-date) . The date here is formatted in german format DD.MM.YYYY. For that I use currently following query:
$args = array(
'post_type' => 'offer',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'offer_start-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '<='
),
array(
'key' => 'offer_end-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '>='
)
)
);
new WP_Query( $args );
Unfortunately, the query does not yield reliable results. I can not even say 100% why. On some days all offers appear, on other days there are no offers.
I have also tried to find out the cause of the problem in the Codex, but it seems I am a strong blockhead.

If you need tu use between just use like that:
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array(date('d/m/Y'), date('d/m/Y', strtotime('28 days'))),
'compare' => 'BETWEEN',
'type' => 'DATE'
),
)

This below solution may be handy to someone
$args = array(
'cat' => $cat_ID,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_from',
'value' => date("Y-m-d H:i:s"),
'compare' => '<=',
'type' => 'DATE'
),
array(
'key' => 'date_to',
'value' => date("Y-m-d H:i:s"),
'compare' => '>=',
'type' => 'DATE'
)
),
'orderby' => 'date',
'order' => 'DESC'
);

Related

Wordpress query based on custom date field and time field

I am using a WordPress Calendar plugin to show multiple events. These events start on different days on different times. The plugin works fine and shows all the the events.
But I wanna display just the future events. E.g., if an event starts at August 1st at 11am, the event should be hidden at the same time. I tried to solve my problem with this query:
add_filter('cmcal_calendar_posts_query', 'calendar_return_specific_events', 10, 2);
function calendar_return_specific_events($query_args, $calendar_id) {
if ($calendar_id == 110) {
$todays_date = current_time('d.m.Y');
$right_now = current_time('H:i');
$query_args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'start_date',
'compare' => '>=',
'value' => $todays_date,
),
'time_clause' => array(
'key' => 'start_time',
'compare' => '>=',
'value' => $right_now,
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
}
return $query_args;
}
But unfortunately it doesn't work. What am I doing wrong?
Thank you for your help.
you should set key 'type' in your 'meta_query' array. The code would be something like this:
add_filter('cmcal_calendar_posts_query', 'calendar_return_specific_events', 10, 2);
function calendar_return_specific_events($query_args, $calendar_id) {
if ($calendar_id == 110) {
$todays_date = current_time('d.m.Y');
$right_now = current_time('H:i');
$query_args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $todays_date,
'type' => 'DATE' //set the format
),
array(
'key' => 'start_time',
'compare' => '>=',
'value' => $right_now,
'type' => 'DATE' //set the format
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
}
return $query_args;
}

Sort event by start date but show until end date has passed on PHP query for Wordpress

I have a question about sorting upcoming events on a website with a PHP query. The issue i am having is that some events run over a couple of days. But I'd like that event to remain visible until the event end date has passed. Right now, once the event start date has passed, the event moves to the bottom of my event list. However, I'd like it to stay in the same chronological order of start date until the end date is over. Hope that makes sense! Any help would be so much appreciated. Here's my query...
<?php
$now = date('Y-m-d H:i:s');
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
'date_upcoming_clause' => array(
'key' => 'event_start_date',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
),
array(
'relation' => 'AND',
'date_started_clause' => array(
'key' => 'event_start_date',
'compare' => '<=',
'value' => $now,
'type' => 'DATETIME'
),
'date_end_clause' => array(
'key' => 'event_end_date',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
),
),
),
'orderby' => array(
'date_started_clause' => 'ASC',
'date_end_clause' => 'ASC',
'date_upcoming_clause' => 'ASC',
),
);
$the_query = new WP_Query($args);
?>

PHP Wildcard to display all values of variable

We have set up custom post types for real estate properties and a search form to search through these custom post types using price, city, newest, etc. We are using query variable strings/if statements to display the results, but we are having trouble with the sort options. We have three sort options in place : Newest, Price Low-High, and Price High-Low.
When the default results are displayed, the sort does not work correctly, but when a city is set in the search dropdown, the sort works perfectly.
For the city search, we have this code in place:
if (isset($_POST['city'])) {
$qs_city = get_query_var('city');
}
else {
$qs_city = " ";
I believe that this is due to the else portion of the code above. We need some sort of wildcard that allows all cities listed to be listed if a city is not set. We have tried several different things, but nothing seems to work. I'm fairly new to this, so I can better clarify any of the above, if necessary.
This is the code used to sort:
if (isset($_POST['sort'])) {
$qs_sort = get_query_var('sort');
switch ($qs_sort) {
case 'newest':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_StatusDate',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-low':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-high':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'DESC',
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
}
}
else {
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_StatusDate',
'orderby' => 'DESC'
//set default to sort by newest
),
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
}
?>
The main question for your situation is "what kind of application will sort the results?". It looks like you use some kind on database. But which one? If it's SQL-like database, - just replace $qs_city = " " with $qs_city = "%";

Date issue in custom post type loop (wordpress)

I am using a custom post type with custom date fields to display tour dates on a page. Some of the posts have both a "from" and "to" date (because they last for more than one day) and some only have "from" (because they're only one day), and I want all future or current tour dates to be displayed.
It's all working perfectly, except for some reason the posts that only have a "from" date stop showing if that date is in 2016 or later. It doesn't make any sense to me!
Here are the args:
$today = date('Ymd');
$tour_args = array(
'post_type' => 'tour_date',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tour-date-from',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tour-date-from',
'value' => $today,
'compare' => '<=',
),
array(
'key' => 'tour-date-to',
'value' => $today,
'compare' => '>=',
),
),
);
This is now solved. The problem was that I needed to specify the meta type. I also needed to take out the 'meta_key' line and therefore amend the 'orderby' line to look for the right value.
$today = date('Ymd');
$tour_args = array(
'post_type' => 'tour_date',
'posts_per_page' => -1,
'orderby' => 'tour-date-from',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tour-date-to',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'tour-date-from',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC'
),
),
);

Date range find in wordpress

In wordpress I have only publish_date(not meta_key) and meta_key "end_date"
Example: event from 2015-06-22(publish_date) to 2015-08-24(meta_ket = "end_date")
now I want search all events
(from 2015-07-05 to 2015-07-24)
or
(from 2015-08-05 to 2015-09-25)
and must show event in example
This code not work:
$opt = array(
'post_type' => 'event_info',
'posts_per_page' => '40',
'paged' => $page_num,
'orderby' => 'post_date ID',
'order' => 'DESC',
'relation' => 'OR',
'meta_query' => array(
'key' => 'end_date',
'value' => array('$date_start','$date_end'),
'compare' => 'BETWEEN',
'type' => 'numeric'),
'date_query' => array(
array(
......
'compare' => 'BETWEEN',
),
),
)
);
relation between meta key and data_query not work,
current idea is create 2 query but I dont want do this
try this : here I added timestamp in startdate and enadate meta key
$chkdt = current_time('timestamp');
$args = array('post_type'=>'event_info', 'post_status' => 'publish','posts_per_page'=>-1,'orderby' => 'ID','order' => 'DESC', 'meta_query' =>
array(
'relation' => 'AND',
/******filter start date and End date****************/
array(
'key' => 'end_date',
'value' => $chkdt,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $chkdt,
'compare' => 'NUMERIC',
),
/******END filter start date and End date****************/
),
);

Categories