Wordpress query based on custom date field and time field - php

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

Related

wc_order_query with meta_query not working when using relation

I am trying to use WC_Order_Query, to get all orders where a custom meta_key doesn't exist, is empty or equal to 0
I've tried like a lot of the stuff documented on this site, but nothing seems to work. It just returns all content, which is the opposite of what i'm trying to do.
This is what most people have recommended so far, but it doesn't seem to work as intended or maybe I am not seeing the issue here
$args = array(
'limit' => 9999,
'return' => 'ids',
'orderby' => 'date',
'order' => 'DESC',
'status' => 'processing',
'date_created' => '>='.$startdate,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
This ended up being the solution:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'the_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => $startdate . $starttime,
'inclusive' => true
)
),
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WP_Query( $args );
$query_posts = $query->get_posts();
Thanks a lot to Boris for the assistance.

Wordpress custom field order by

I'm trying to order by the custom field InPrice but not able to do that. I'd know if possible in another way makes it ('orderby' => 'meta_value_num', 'order' => 'ASC',)
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'meta_key' => 'InPrice',
'meta_value' => array( $pieces[0], $pieces[1]),
'meta_type' => 'numeric',
'meta_compare' => 'BETWEEN',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'btown' => array(
'key' => 'Town',
'value' => $town,
'type' => 'STRING',
'compare' => 'LIKE',
),
broom' => array(
'key' => 'Rooms',
'value' => $rooms,
'compare' => 'LIKE',
),
),
),
);
referred to Wordpress Documentation
you can use something like code below :
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'age',
'value' => array( array( $pieces[0], $pieces[1]) ),
'compare' => 'IN',
), // other meta queries that you want
//don't forget to set a relation type for them in here ex:'relation'=>'AND',
),
);
$query = new WP_Query( $args );

How to query posts which have more than one specific tags?

I want to query specific posts which have both $apple and $orange tags.
Using tag__in returns the list of posts which have either $apple or $orange tags.
But I want the posts which have them both.
Here's my code:
$apple = 276; // tag id of Apple
$orange = 29; // tag id of orange
$args = array(
'post_type' => array(
'post',
'review',
'cardguide'
),
'taxonomy' => 'post_tag',
'posts_per_page'=> - 1,
'tag__in' => array(
$apple,
$orange
),
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'event_end_date',
'meta_query' => array(
'relation'=> 'AND',
array(
'key' => 'event_start_date',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
array(
'key' => 'event_end_date',
'value' => $today,
'compare'=> '>=',
'type' => 'DATE'
)
)
);
Since I already have a meta_query here, so I am kind of confused how to modify it to get the expected output. Adding new arrays for those tags with = operator to compare doesn't work wither.
'meta_query' => array(
'relation'=> 'AND',
array(
'key' => 'event_start_date',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
array(
'key' => 'event_end_date',
'value' => $today,
'compare'=> '>=',
'type' => 'DATE'
),
array(
'key' => 'tag_id',
'value' => $apple,
'compare'=> '=',
),
array(
'key' => 'tag_id',
'value' => $orange,
'compare'=> '=',
),
)
I think you should use a tax-query instead of meta-query.
So your code will be something like:
$taxQuery = array('relation' => 'AND');
array_push($taxQuery,array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $apple,
));
array_push($taxQuery,array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $orange,
));
// And add in your args like tax-query
$args = array(
'post_type' => array(
'post',
'review',
'cardguide'
),
'posts_per_page'=> - 1,
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'event_end_date',
'meta_query' => array(
'relation'=> 'AND',
array(
'key' => 'event_start_date',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
array(
'key' => 'event_end_date',
'value' => $today,
'compare'=> '>=',
'type' => 'DATE'
)
),
'tax_query' => $taxQuery
);
Why don't you try something like this:
$pics = DB::table('pictures')
foreach ($pics as $pica) {
if ($pica->category !== 1)
{
continue;
}
echo ...

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

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