Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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'
);
query_posts($query); 
share|improve this question

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>
ORDER BY ddiv

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

 
discard

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.