I’m struggling to get a working solution with this wp_query
. I currently have some custom settings which are assigned to posts, one is whether or not the post is ‘featured’ and the second is a date and time for the post to end (no longer display in the results). I have the query working with the feature, but just need to work this end date into it, here is the query working find with the ‘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 the meta_values
look like this ’05/16/2013 05:24′. I would like to edit the above query where if ‘the_date’ has been set posts are only included if the ‘the_date’ is greater that todays date and time.
Here is my failed attempt:
WP_Query(
'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value='
.date('d/m/Y H:i')
.'&showposts=4&orderby=post_date&orderby=the_date'
);
I had to do something very similar recently and ended up needing to use the
meta_query
property instead. You’ll want to do something like this:A few notes…
I only needed to filter by date in my example, but it looks like you’ll need to do date/time in yours. (You can just adjust the first line for the
$today
variable using the format you wish).Use
posts_per_page
instead ofshowposts
.showposts
is deprecated.Notice that I have included the
meta_key
twice (once at the top level of the array and once as an element in themeta_query
array. There’s a known bug where you can’t sort your results by the key if you don’t include it this way. I fought that one for a while too!Hope this helps, have fun!
[edit] Forgot to add your
skyali_feature
key back into the array.for people using the advanced custom field plugin with a date data type, this is what you need for dates greater or equal than today:
for people using Custom metadata manager you’ll find that a
datepicker
field is stored as timestamp.So in a similar case the above example isn’t working and you may have php sort out the value you need to compare against. And the timestamp for a day earlier at 23:59:59 it’ll do the job:
If you also want to take account of the timezone setting for the blog use current_time() as in the following example: