Speed optimization of $wpdb->get_results

Your query is incorrect in the first place because you’re not doing the join correctly. What you’re actually selecting with that query is just all the postmeta’s autor fields, without regards to whether the post is published or not.

Here’s the corrected query.

SELECT meta_value AS autor FROM wp_posts 
JOIN wp_postmeta ON (wp_postmeta.post_id =  wp_posts.ID) 
WHERE post_status="publish" 
    AND meta_key = 'autor' 
GROUP BY meta_value 
ORDER BY meta_value

This will probably be faster because the proper join will eliminate a lot of the posts. It’s not fully optimal, but it will give some improvement.

Leave a Comment