i need to get post and featured image with specific category id in wordpress by custom query. This is my query:

SELECT p.ID, post_title AS title, post_excerpt AS excerpt,t.name AS name_cat ,bb.meta_value AS url_image FROM ap_posts p 
    SELECT p1.ID,wm2.meta_value FROM ap_posts p1 
    LEFT JOIN ap_postmeta wm1 ON ( wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = '_thumbnail_id' ) 
    LEFT JOIN ap_postmeta wm2 ON ( wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file' AND wm2.meta_value IS NOT NULL ) ) AS bb ON p.ID = bb.ID 
    JOIN ap_term_relationships tr ON (p.ID = tr.object_id) 
    JOIN ap_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
    JOIN ap_terms t ON (tt.term_id = t.term_id) WHERE p.post_type='post' AND p.post_status = 'publish' AND tt.taxonomy = 'category' AND t.term_id =$cat_id ORDER BY post_date DESC 

The result is ok, but it run slow because i use many “JOIN”. How can i optimize this sql to increase performance. Thanks

Read more here: i need to get post, featured image with specific category id wordpress by custom query


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: