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!
Related
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.
I am trying to run a WP_Query in WordPress to retrieve posts dated after today using a custom field. My key _mcd_event_date_end has values stored in the m/d/Y format (02/16/2016).
$args = array(
'order' => 'DESC',
'posts_per_page' => -1,
'post_type' => 'mcdevent'
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_mcd_event_date_end',
'value' => date("m/d/Y"),
'compare' => '>=',
),
array(
'key' => '_mcd_event_type',
'value' => 'Other Event',
'compare' => '=',
),
),
);
$other_events = new WP_Query($args);
wp_reset_postdata();
This is the code I am running, and it works for dates posted this year, but not prior. Meaning if I have a post with the _mcd_event_date_end key having a value of something like 4/16/2016 it will show up and if it is something like 2/12/2016 it will not show up. But then anything from 12/31/2015 and before also shows up.
Thanks for your help and please let me know if there's any more information I can provide.
You should be using "date" for the custom field type:
array(
'key' => '_mcd_event_date_end',
'value' => date('Y-m-d H:i:s'),
'compare' => '>=',
'type' => 'DATE'
),
But I think the data must be in "YYYY-MM-DD" format for it to work.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
In wp_query, is it possible to build a complex query so I can order by a conditional or calculated fields?
what I'm trying to do is something like the next query in MySql:
SELECT *, field1, field2
case when field1+field2 > some_value then 1 else 2 end as my_alias
FROM my_table
ORDER BY my_alias ASC
I want to build queries like this one using wp_query, is this possible? if yes, how can I accomplish that?
Yeah, you need to
add custom field using
https://developer.wordpress.org/reference/hooks/posts_fields/
add custom order by using
https://developer.wordpress.org/reference/hooks/posts_orderby/
I don't see any way to do this with a single WP_Query as meta_query doesn't allow you such flexibility, though you can do three distinct queries then merge them (untested code):
// Get ongoing events
$ongoing = new WP_Query(array(
'post_type' => 'event',
'meta_key' => 'date_from',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_from',
'value' => date('Y-m-d'),
'compare' => '<=',
'type' => 'DATE'
),
array(
'key' => 'date_to',
'value' => date('Y-m-d'),
'compare' => '>=',
'type' => 'DATE'
)
)
));
foreach($ongoing as $key => $ongoing_post) {
$ongoing_post->event_status = 'ongoing';
$ongoing[$key] = $ongoing_post;
}
// Get upcoming events
$upcoming = new WP_Query(array(
'post_type' => 'event',
'meta_key' => 'date_from',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_from',
'value' => date('Y-m-d'),
'compare' => '>',
'type' => 'DATE'
)
)
));
foreach($upcoming as $key => $upcoming_post) {
$upcoming_post->event_status = 'upcoming';
$upcoming[$key] = $upcoming_post;
}
// Get past events
$past = new WP_Query(array(
'post_type' => 'event',
'meta_key' => 'date_from',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'date_to',
'value' => date('Y-m-d'),
'compare' => '<',
'type' => 'DATE'
)
)
));
foreach($past as $key => $past_post) {
$past_post->past_status = 'past';
$past[$key] = $past_post;
}
// Merge'em all
$events = array_merge($ongoing, $upcoming, $past);
The thing is to use meta_query to compare the meta values with the actual date (you may want to change the date format depending of how they are stored in date_from and date_to fields), and do a little loop right after to add a property to all post object with the right event_status which you can work with when displaying posts.
Maybe there is a clever way to achieve this through WP_Query filters but it would need more in-depth investigation inside WP_Query source code as it is not really documented inside the codex.
I thing You use, and try this,
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT *, field1, field2 case when field1+field2 > some_value then 1 else 2 end as my_alias FROM my_table ORDER BY my_alias ASC");
print_r($result);
?>
Look at this Click Here
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
);