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
Related
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.
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;
}
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 );
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****************/
),
);
I'm attempting to output a list of WordPress users using new WP_User_Query( $args ). The list is made up only of 'Special Members' and 'Superspecial Members':
I am currently using the following 'OR' relation meta_query as my $args to achieve this:
// the Args
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'access',
'value' => 'Special Member',
'compare' => '=',
),
array(
'key' => 'access',
'value' => 'Superspecial Member',
'compare' => '=',
),
)
);
I'd also like to order the outputted list by user last name (as below). Is there some way to add this to the above set of args as an 'And' relation? I have tried appending to the 'Superspecial Member' array, but this breaks the code.
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'last_name',
);
Any ideas?
Don't you want to just do something like this?
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'last_name',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'access',
'value' => 'Special Member',
'compare' => '=',
),
array(
'key' => 'access',
'value' => 'Superspecial Member',
'compare' => '=',
)
)
);