I am using the following query:

<?php $postslist = new WP_Query('meta_key=Ordermetakey&orderby=meta_value_num&order=ASC&posts_per_page=5');

To sort a small post list on the frontpage to show latest new. It needs to order them by a meta key and do this DESC. The strange thing is it shows the posts ASC even though i have put ASC in this query.

The value of the meta key is a date - time stamp: Example: 120403 = 03-04-2012

How can i make it work?

  • You must put DESC in your query of course but I believe you have another problem. 120403 is not a timestamp. The best solution is to save the date as a timestamp. – offroff May 4 '12 at 11:27
  • If sorting by date use a time-stamp or store the date in YYYYMMDD format. The format you are using won't sort correctly. Consider the 11th November 2011 and 1st January 2012 . – Stephen Harris May 4 '12 at 12:34
  • But i currently use YYMMDD and it works with the Smart Sort plugin. – ruben May 4 '12 at 12:52
  • Just because you are doing it now and it works in a specific case does not make it the best way to do things. You should be storing your date as a unix timestamp, which is the number of seconds since the unix epoch. This is the most reliable way to sort dates and the most flexible format for the data. – mor7ifer May 4 '12 at 13:53
  • Yes but my question is why does my WP Query cant order things by a custom field. – ruben May 4 '12 at 14:03

if you want to order it DESC put DESC in your query.

$postslist = new WP_Query('meta_key=Ordermetakey&orderby=meta_value_num&order=DESC&posts_per_page=5');
  • Yes was an example. Also tried DESC but it just shows ASC. – ruben May 4 '12 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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