5

I am having an issue that i couldn't find on google and elsewhere. To put problem in the context. I am making a wordpress site where user can put their tutor listing for giving a tutorials, and i am implementing the system of evaluation of the tutors them self and their listening. So I have tutor_listing post type and each tutor_listing has a 'post_score' meta value, and each tutor (user who puts his tutorials) has user_meta value called 'user_points'.

So I need a WP_Query that will get from the database all published tutor_listing but sorted by (post_score + user_points). Just to emphasis 'post_score' is a post_meta of the post_type tutor_listing and 'user_points' is user_meta of author of that post (tutor_listing). Also i need that value to show in the loop.

I can get all the tutor_listing by

$args = array(
    'post_type' => 'job_listing',
    'post_status' => 'publish'
);
$query = new WP_Query( $args );

I can get the result from the custom wordpress sql query like this:

SELECT p1.ID, p1.post_author, p1.post_title, p1.post_date, (um1.meta_value + pm1.meta_value) AS total_score
FROM $wpdb->posts p1
    JOIN $wpdb->users u1 ON (p1.post_author = u1.ID)
    JOIN $wpdb->usermeta um1 ON (um1.user_id = u1.ID)
    JOIN $wpdb->postmeta pm1 ON (pm1.post_id = p1.ID)
WHERE p1.post_type = 'tutor_listing'
AND p1.post_status = 'publish'
AND um1.meta_key = 'user_points'
AND pm1.meta_key = 'post_score'";

Can WP_Query provide such a functionality? I need WP_Query to use its all extra functionality like pagination ect. Or is there a way i could all the result done with the my custom sql give to WP_Query object?

Thank you

4
  • If you use $wpdb you could get the result of the sql above as an object. codex.wordpress.org/Class_Reference/wpdb It's the only way I found to run custom queries, and still getting all the results inside a wp formatted object.
    – gdaniel
    May 13, 2015 at 23:40
  • Thank you @gdaniel. Unfortunately with $wpdb->get_results($querystr, OBJECT_K); or just OBJECT i cannot have the values that i need for pagination. The thing that i need is, when making the query i could set LIMIT and OFFSET. That will give me only the LIMIT number of the post from the database but i dont have the number of all the posts that meet the query criteria. WP_Query holds that information. May 15, 2015 at 3:44
  • Here is solution how to make a pagination for $wpdb queries with $wp_query. Solution is on this post May 18, 2015 at 17:09
  • don't know if this helps, but have you looked at the meta query arg? 'orderby' => 'meta_value', 'meta_key' => 'post_score'. both orderby and meta_key can be arrays
    – thebigtine
    Dec 22, 2015 at 9:59

2 Answers 2

1

WordPress 4.0 allowed you to specify multiple orderby parameters and set the order of each independently.

How to do it? Just make multiple refferences, here is one way to do that.

$args = array(
    'orderby' = > array(
        'title' => 'ASC',
        'date' => 'DESC',
    )
);

Learn more here...

But only since WordPress 4.2 it is possible to do this with meta values. Have a look at this post.

0

You can use wpdb and still get pagination functionality. Here is a good answer on the topic:

WordPress Paginate $wpdb->get_results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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