1

I have the following WP user query code:

//Get all users sorted by whatever
$sorted_users = new WP_User_Query(array(
    'orderby' => 'meta_value',
    'meta_key' => 'satisfaction_rating',
    'order' => $order,
    'role' => 'subscriber',
    'meta_query' => array(
        array(
            'key' => 'satisfaction_rating',
            'type' => 'NUMERIC'
        )
    )
));

//Set up our search fields
$search = new WP_User_Query(array(
    'orderby' => $order_by,
    'order' => $order,
    'role' => 'subscriber',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'description',
            'value' => $search,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'full_name',
            'value' => $search,
            'compare' => 'LIKE'
        )
    )
));

//Get users filtered
$filtered_users = new WP_User_Query(array(
    'role' => 'subscriber',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'hourly_rate',
            'value' => array($filter_values['min_hourly_rate'], $filter_values['max_hourly_rate']),
            'compare' => 'BETWEEN',
            'type' => 'UNSIGNED'
        ),
        array(
            'key' => 'satisfaction_rating',
            'value' => array($filter_values['min_satisfaction_rating'], $filter_values['max_satisfaction_rating']),
            'compare' => 'BETWEEN',
            'type' => 'UNSIGNED'
        ),
        array(
            'key' => 'country',
            'value' => $filter_values['country'],
            'compare' => ($filter_values['country'] == '' ? 'LIKE' : '=')
        )
    )
));

I then use a foreach loop to iterate over the sorted users array and drop any values that aren't present in one of the other queries. When scaled up, however, the execution time on this is very high due to the number of queries. I know that a direct SQL statement would be faster, but my SQL-fu is not very strong. I believe that I have to use joins to get the desired effect, as I need to get info both from meta and from the users table, but I don't know how I would begin to structure the statement. Can anybody give me a push in the right direction?

Edit:

Same thing now as a nested query:

$users = new WP_User_Query(array(
    'orderby' => $order_by,
    'order' => $order,
    'role' => 'subscriber',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'hourly_rate',
            'value' => array($filter_values['min_hourly_rate'], $filter_values['max_hourly_rate']),
            'compare' => 'BETWEEN',
            'type' => 'UNSIGNED'
        ),
        array(
            'key' => 'satisfaction_rating',
            'value' => array($filter_values['min_satisfaction_rating'], $filter_values['max_satisfaction_rating']),
            'compare' => 'BETWEEN',
            'type' => 'UNSIGNED'
        ),
        array(
            'key' => 'country',
            'value' => $filter_values['country'],
            'compare' => ($filter_values['country'] == '' ? 'LIKE' : '=')
        ),
        array(
            'relation' => 'OR',
            array(
                'key' => 'description',
                'value' => $search,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'full_name',
                'value' => $search,
                'compare' => 'LIKE'
            )
        )
    )
));

I have a large number of users (1500+) on the site and this query is taking a good bit of time to run (5+ seconds). How can I cut down on execution time?

  • Wordpress introduced nested queries in v4.1 so you should be able to merge this into a single query without much trouble: make.wordpress.org/core/2014/10/20/…. – Joey Yax Feb 24 '15 at 22:24
  • Awesome sauce. I'm trying it now. – Thought Space Designs Feb 24 '15 at 22:48
  • So I tried nested queries, but they're taking a large amount of time to run (5+ seconds). Any idea how I can speed up execution some? – Thought Space Designs Feb 24 '15 at 23:04
  • I wouldn't expect the query to take nearly that long. You're sure it's the query and not the foreach loop? – Joey Yax Feb 25 '15 at 0:22
  • I stuck a start time variable before the WP_User_Query and an end time variable right after it, calculated the difference and then spit it out and it came out to 5 ish seconds. – Thought Space Designs Feb 25 '15 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.