1

Hi i'd like to order by multiple meta_keys

want to make it order using these keys

adminscore + user_like + _count-views_all + comment_count

combine these keys how should i array order it??

i try this but not work..

  $args = array(
            'meta_key' => 'adminscore' + 'user_like' + '_count-views_all' + 'comment_count',
                'numberposts' => $number_posts,
                'post_status' => $post_status,
                'post_type' => $custom_post_type,
                'orderby' => 'meta_value_num'
               );

Thanks for check my question, hope someone has answer for it.

  • Are you adding up the values? – Rutwick Gangurde Dec 9 '12 at 5:55
  • yes.. i like to add up values and orderby from it. – Jido Park Dec 9 '12 at 15:14
  • Then you need to add the values and store it as a separate meta. Then use it for ordering. Check the @Syamil MJ's answer. – Rutwick Gangurde Dec 10 '12 at 4:54
3

This line is wrong:

'meta_key' => 'adminscore' + 'user_like' + '_count-views_all' + 'comment_count',

'meta_key' only accepts string value.

What you need is an additional meta key, e.g. "totalscore" that holds the total value of the scores you have for all the meta values. You can manually run a php function that calculates and store the totalscore value from time to time or use wp_schedule_event to run the function on a set interval.

You can then write the $args as such:

$args = array(
            'meta_key' => 'totalscore',
            'numberposts' => $number_posts,
            'post_status' => $post_status,
            'post_type' => $custom_post_type,
            'orderby' => 'meta_value_num'
);
| improve this answer | |
  • Exactly. Plus one. – Rutwick Gangurde Dec 10 '12 at 4:54
  • Hi, Syamil thanks for solve my question. the 'wp_schedule_event' could be a way that solve this problem. where should I locate the code? in function.php? and.. could you make any sample code to save new meta value(totalscore) from add up meta values such as view count, user_like_ comment_count using the 'wp_schedule_event'. – Jido Park Dec 10 '12 at 17:07
1

The only thing you can do here is to use the posts_orderby filter

To make sure you don't modify any other queries, use the add_filter call just before retrieving the posts & a remove_filter call either in the hooked function itself or just after you have the posts.

| improve this answer | |
  • Hi, Mridul thanks for answering. i don't under stand using filter.. do you have any code example?; – Jido Park Dec 10 '12 at 16:56
  • The link in the answer already has an example to get you started. For a more complete reference, you can go through this codex – Mridul Aggarwal Dec 10 '12 at 19:37

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.