Request for messages, including meta and more than date

I am trying to get a working solution with this wp_query . Currently, I have some user settings that are assigned to messages, one of them is a β€œsign”, and the second is the date and time the recording ended (no longer displayed in the results). I have a query working with this function, but I just need to work with this end date, here is a search job using "featured":

 WP_Query('meta_key=skyali_feature&showposts=4&orderby=post_date'); 

The end date is set in the wp_postmeta table, where meta_key is' ​​the_date 'and meta_values is '05 / meta_values 05:24'. I would like to edit the above query, where if the given messages "the_date" are included only if "the_date" is more than the current day and time.

Here is my unsuccessful attempt:

 WP_Query( 'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value=' .date('d/m/YH:i') .'&showposts=4&orderby=post_date&orderby=the_date' ); 
+8
date wordpress
source share
3 answers

Recently, I had to do something very similar, and I had to use the meta_query property. You want to do something like this:

 $today = date('Ymd'); $args = array( 'post_type' => 'post', 'posts_per_page' => '4', 'meta_key' => 'the_date', 'meta_query' => array( array( 'key' => 'skyali_feature' ), array( 'key' => 'the_date', 'value' => $today, 'compare' => '>=' ) ), 'orderby' => 'meta_value_num', 'order' => 'ASC' ); $your_custom_query = new WP_Query($args); 

A few notes ...

  • I only needed to filter by date in my example, but it looks like you will need to do the date / time in yours. (You can simply adjust the first line for the $today variable using the format you want).

  • Use posts_per_page instead of showposts . showposts deprecated.

  • Note that I turned on meta_key twice (once at the top level of the array and once as an element in the meta_query array). There is a known error in which you cannot sort the results by key, if you do not enable it in this way. I also fought for a long time with this!

Hope this helps, have fun!

[edit] Forgot to add the skyali_feature key back to the array.

+13
source share

for people using an advanced custom field plugin with date data type, this is what you need for dates greater than or equal to today:

  $today = date('Ymd'); $args = array( 'post_type' => 'post', 'meta_key' => 'end-date', 'meta_query' => array( array( 'key' => 'end-date' ), array( 'key' => 'end-date', 'value' => $today, 'compare' => '>=' ) ), 'orderby' => 'meta_value', 'order' => 'ASC' ); $your_custom_query = new WP_Query($args); 
+4
source share

for people using the User Metadata Manager , you will find that the datepicker field is stored as a timestamp.

Thus, in a similar case, the above example does not work, and you can configure php to the value you need to compare. And marking the day a day earlier at 23:59:59 he will do the job:

  $yesterday = strtotime('yesterday 23:59:59'); $args = array( 'post_type' => 'post', 'meta_key' => 'end-date', 'meta_query' => array( array( 'key' => 'end-date' ), array( 'key' => 'end-date', 'value' => $yesterday, 'compare' => '>=' ) ), 'orderby' => 'meta_value', 'order' => 'ASC' ); $your_custom_query = new WP_Query($args); 

If you also want to consider the time zone setting for the blog, use current_time () , as in the following example:

  $now = current_time('timestamp'); $yesterday = mktime(23, 59, 59, date('n',$now), date('j',$now)-1); 
+1
source share

All Articles