-1

I have 2 meta fields for the posts, eg. meta_value and meta_count. Both are numbers.

I want to divide meta_value by meta_count and based on the results I want to fetch the top 10 posts.

So for example, post 1 has the meta_value 5, and meta_count 2. by dividing the result for that post will be 2.5.

And the post 2 has meta_value 1 and meta_count 2, its result will be 0.5.

I know there is a way to order posts by a specific meta key:

'meta_key' => 'meta_value',
'orderby' => 'meta_value_num',

But I do not know if it is possible what I'm trying to do.

0

Assuming meta_count is another column in your postmeta table below query should get you the posts based on meta_value/meta_count calculation.

$postids=$wpdb->get_col( $wpdb->prepare( 
    "SELECT post_id
    FROM $wpdb->postmeta
    ORDER BY (meta_value/meta_count)
    LIMIT 10
    "
) );

The result $postids would be an array. You will have to declare $wpdb as a global variable before you use this query.

0

Have you tried wp_query to solve this problem. I am not sure how you are trying to retrieve top 10 post but so far that I have understood this can be solved with WP_query.

Refer this: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

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.