I'm doing a WP_Query with a date_query parameter after, and I want the nearest post after that date, but I get the farest ones in the future from the date. Is there any parameter to control this? something like 'near_to' => 'past/future', or WP_Query always retrieve the most recent? This is my code:

$my_query = new WP_Query(array(
    'post_type' => 'event',
    'posts_per_page' => 1,
    'date_query' => array(
        'after' => date('Y-m-d h:i',time())

I have an events site, and I want to show the next one, not the farest one.

Ignore the fact that the date checked is today and the post that I search for are in the future. It doesn't matter (no?).

I have been loking at the Codex but I didn't find anything. Have I to query for ALL post and the go to the last one in the $my_query->posts array?

Edit: I don't want to order my results, I want to limit them by older posts, not by newest (default). I have several posts with a future date (events), and I want the next event. If I do a query with only this parameter 'posts_per_page' => 1 I get the latest post, and I want the oldest.


The solution is to use the parameter 'order' => 'ASC'. Thanks to @PieterGoosen and @webtoure. Now let me explain why my question was not 100% correct, and why it works.

I didn't test with 'order' because I only have 1 wrong post (that was I thought), I had nothing to order, but WP_Query does not works that way. As I myself explained on this answer, WP_Query first finds the posts that match the criteria, and then orders them, do the pagination, etc. So you can search for posts 'after' a date (all of them), order then by 'ASC', and get only 1 post with 'posts_per_page' => 1.

  • Your question is ambiguous. Are the post dates in the future, or are these dates you are talking about dates in a custom fields. File an edit with this required info Aug 12, 2015 at 10:41
  • @PieterGoosen The dates that I'm am talking about are the post_date. That's why I am talking about date_query, and why I don't mention custom fields. I use the No Future Posts plugin.
    – EliasNS
    Aug 12, 2015 at 10:47
  • You see, missing info :-). What does the plugin do, how does it change post dates and post status. Normally posts in the future have a status of future, so you need to change your post status parameter then Aug 12, 2015 at 11:01
  • @PieterGoosen My question has nothing to do with post status. My code is working fine and give me posts, but newer instead of older. I think you guys are not understanding the question (because it's strange).
    – EliasNS
    Aug 12, 2015 at 11:07
  • 1
    If you want the oldest post, then you need 'order' => 'ASC' Aug 12, 2015 at 11:20

1 Answer 1


Are you actually using 'after' => date('Y-m-d h:i',time()) in your code? That will always ask for posts older newer that the exact current time when that query is run. Something like this:

'after' => date( 'Y-m-d h:i', time() - 60 * 60 * 24 )

would give you a reference for 24 hours in the past.

The above part is for history's sake. The solution in this answer is and was right all along though.

As for the results being "backwords" (newest to oldest instead of vice versa) you might want to add an order clause to your snippet (I haven't tested it):

$my_query = new WP_Query( array(
    'post_type' => 'post',
    'order' => 'ASC',
    'posts_per_page' => 1,
    'date_query' => array(
            'after' => date( 'Y-m-d h:i', time() ),
) );

Final edit (tested and working). You need the order clause since WordPress queries posts 'DESC' by default. Test the code and see for yourself.

  • Good answer. I would add that date_query accepts relatives dates. For example 'after' => '-30 days' for last month.
    – cybmeta
    Aug 12, 2015 at 9:26
  • I know the order parameter, that is not what I want to do. I have added information about it in my question. After gets newer posts, not older (after the date especified): Codex. $wp_query was only an example (bad one) and is not my real variable. @cybmeta 30 days is not always a month.
    – EliasNS
    Aug 12, 2015 at 10:20
  • 1
    @EliasNS Use the order clause. End of story. :)
    – webtoure
    Aug 12, 2015 at 11:13
  • 1
    @EliasNS you are confusing me; a lot. From your question and your comments, this is the answer to your problem. Even you have said that the solutin is to user the order parameter. But it seems that we don't understand you at all. Also, you are incorrect in the explanation of why this answer is not 100% correct. The order parameter is set to DESC by default, It doens't matter if you want to order the results or not, they are ordered already but to make WP_Query work as you want you HAVE TO CHANGE IT TO ASC. That's all and that is what webtoure said to you and what solved your problem.
    – cybmeta
    Aug 12, 2015 at 13:55
  • 1
    @cybmeta Thank you for being the one clearing this up. To my disappointment, another privileged user on this thread didn't want to make it clear that this is the final correct solution and somewhat deepened the confusion with another answer as if this one wasn't correct.
    – webtoure
    Aug 12, 2015 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.