WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to both PHP and WP so I am struggling with one thing:

I want to fetch all my post and then order it according to my meta-value "_mycounter" divided with the date in seconds from 1970 or what ever it was.

I know 'orderby' => 'meta_value_num / date' is not right but how can I solve this? Is there anyway to add division/multiplication calculation in the way I am asking for?

$query = array(
  'orderby' => 'meta_value_num / date', 
  'meta_key' => '_mycounter',
  'order' => 'DESC'
share|improve this question

MySQL can do math but can't pass it through query_posts like that, or any WordPress' post query functions, which depend upon WP_Query. They will literally look for a table/column named, in your case, "meta_value_num / date". To do it in SQL I believe-- aka., this is untested-- you'd have to do something like this:

SELECT (meta_value_num / DATE()) as ddiv
FROM <some-table>

And that isn't going to be a very efficient query anyway.

Why can't you do the division after the query? I believe the sort order will be the same either way.

share|improve this answer
How would I do the division AFTER the query? Can anyone give one simple example? – Random_T Jan 15 '13 at 6:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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