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'

1 Answer 1


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.

  • How would I do the division AFTER the query? Can anyone give one simple example?
    – Random_T
    Jan 15, 2013 at 6:28

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.