I am trying to loop some posts, that have a custom field called "WooCommerceEventsDate" containing a date in format (j F Y).
What I'm trying to achieve is only looping posts that are after the current date. I have tried just about any solution I've seen online and I just can't get it to work. Right now the loop takes all posts, and not just the ones that have a date after today.
Here are my arguments so far:
$today = date('j F Y');
$args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'WooCommerceEventsDate',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'WooCommerceEventsDate',
'compare' => '>=',
'value' => $today,
)
)
);
Can one of you guys spot where this is going wrong, and why the arguments arent filtering?
For clarification
echo get_field('WooCommerceEventsDate'); echoes: "2 August 2016".
I am unable to change the output of WooCommerceEventsDate in my backend. I can only do it in my own code.
You can pass the type in meta_query.
type => (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
$today = date('j F Y');
$args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'WooCommerceEventsDate',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'WooCommerceEventsDate',
'compare' => '>=',
'value' => $today,
'type' => 'DATE',
)
)
);
For more information Check this http://www.billerickson.net/code/wp_query-arguments/ line no 141 'meta_query'
EDIT
For Change date format of WooCommerceEventsDate Go to plugins/woocommerce_events/js/events-admin.js find this function
jQuery('#WooCommerceEventsDate').datepicker({
dateFormat : 'd MM yy'
});
Change with this
jQuery('#WooCommerceEventsDate').datepicker({
//dateFormat : 'd MM yy'
dateFormat : 'yy-mm-dd'
});
Note::I am not sure this change affects to another or not.
Related
$todayDate = date('m/d/Y');
$futureDate = strtotime ( '+7 days' , strtotime ( $todayDate ) ) ;
$futureDate = date ( 'm/d/Y' , $futureDate );
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'event_category' => 'events',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $futureDate,
)
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
I want to show the upcoming events for the next week or next month. I have this args array set but still giving me the post of yesterday's and not sorted as how i wanted. Also, how to get rid of yesterday's post in my query..
So if you want your args to limit events that fall between today and the future date, where future date is today +7 days.
Hence you want to modify your args to include another array with the following parameters. Try this:
//event_date >= $todayDate
//event_date <= $futureDate
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'event_category' => 'events',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $todayDate,
),
array(
'key' => 'event_date',
'compare' => '<=',
'value' => $futureDate,
),
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
I'm using a custom post type for events, and trying to display events happening between now and 30 days from now. Pulling all the dates correctly, but I can't seem to get a range of dates into my comparison operators. Here's where I am:
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '<',
'value' => $nextMonthDate,
'type' => 'DATETIME'
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'event_date',
'meta_type' => 'DATETIME'
));
I also have a $date_now variable. Essentially I need an if (event_date >= $date_now && event_date < $nextMonthDate), but in this array, which I apparently need to use to pull other post data.
Thanks!
You can use multiple meta_query conditions as well. I think the following following snippet will help you achieve your expected filter. But didn't test it
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_date',
'value' => $now,
'type' => 'DATETIME',
'compare' => '>=',
),
array(
'key' => 'event_date',
'value' => $nextMonthDate,
'type' => 'DATETIME',
'compare' => '<',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'event_date',
'meta_type' => 'DATETIME',
));
Note the $now I used, just pass on the current date there.
Extra
I think you can also use BETWEEN as compare operator and pass on an array of two items where first one is min and second one is maximum. I am not sure if this will work with DATETIME type or not but it surely does work with NUMERIC types.
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array( $now, $nextMonthDate ),
'type' => 'DATETIME'
'compare' => 'BETWEEN',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'event_date',
'meta_type' => 'DATETIME',
));
Problem
I am looping through custom post types (Advanced Custom Fields) in Wordpress. I only want to display events with start_date equal to $newdate variable, defined in the beginning.
The start_date is in the format YYYY-MM-DD HH:mm (the same as $newdate). $newdate is set to the beginning of the day so I wouldn't exclude events with different hours in the day and compare is set to greater than (just to test the query).
However I am not getting any results.
<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => $newdate,
'compare' => '>=',
'type' => 'datetime'
)
)
);
$loop = new WP_Query( $args );
Try this query:-
'meta_key' => 'event-start-date',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => date('Ymd',strtotime($newdate)),
'compare' => '>=',
'type' => 'date'
)
)
I guess you have some small mistakes here.
First, if you use 'orderby' => 'meta_value' you must add a 'meta_key' => KEYVALUE to your $args where KEYVALUE is the name of the custom field you want to use for sorting. Check WP_Query documentation on Order & Orderby Parameters.
Second, assuming your start_date field type is Date Time Picker, if you want to get all events that already started you're using the wrong comparison operator. So assuming you also want to sort by date, your $args code should be:
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'start_time',
'meta_type' => 'DATETIME'
'meta_query' => array (
array(
'key' => 'start_time',
'compare' => '<=',
'value' => $newdate,
'type' => 'DATETIME'
)
)
);
Take a look at Date Time Picker documentation too.
I hope this helps!
I have some posts with a custom filed named "event_end_date". I am using Advanced custom fields for that. How can I get the posts which have end_date older than today? I was trying with this, but could not get through.
$args = array(
'cat' => '6, -33',
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'event_end_date',
'value' => date('y-m-d'),
'type' => 'DATE',
'compare' => '>'
)
)
);
//get results
$the_query = new WP_Query( $args );
Here I wanted those posts which have category '6' and does not have category '33'. In the backend, the field - 'event_end_date' has these:
Field Type: Date Picker
Display format: 10/11/2014
Return format: 20141110
I think if you were too look more closely at the data being stored in that actual custom/meta field, you'd find that it's stored as a unix timestamp, not in the return or display format, and because of that, your problem is that your format's don't match up.
You can account for that difference by using the U code for unix timestamp with date('U') instead of date('y-m-d').
In that case your code would look like this:
$args = array(
'cat' => '6,-33',
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'event_end_date',
'value' => date('U'),
'type' => 'DATE',
'compare' => '>'
)
)
);
//get results
$the_query = new WP_Query( $args );
Using date('Y-m-d', time()) solved the problem for me.
$args = array(
'category__and' => array(6),
'category__not_in' => array(33),
'numberposts' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'event_end_date',
'value' => date('Y-m-d', time()),
'type' => 'DATE',
'compare' => '<'
)
)
);
I have 3 meta fields, date (date formatted string), location, and consultant.
I can pull the posts and orderby the date field when meta_key is set:
// get posts
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
However, I have not be able to get this to work with a more complex meta_query:
$args = array(
'post_type' => 'uppy_events',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => $key,
'value' => array($value),
'compare' => '>='
)
),
'numberposts' => -1
);
Basically I want to pull all posts that have a meta start_date greater than today's date and order by these dates.
As a side note - can you do a meta_query search on say location key and still orderby date key?
This is the first time I've ever tried ordering by meta, and have found some good posts but still cannot wrap my head around it.
OK, I figured this out as my post lead me to another good example. The commented out meta_query is date greater than today. The other is using a different key. Both work!
$today = date("Ymd");
$args = array(
'post_type' => 'uppy_events',
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
/* array(
'key' => 'event_date',
'value' => $today,
'compare' => '>='
) */
array(
'key' => 'event_location',
'value' => 'vancouver',
'compare' => '='
)
),
'numberposts' => -1
);