Site icon Hip-Hop Website Design and Development

Use WP_Query with a custom SQL query

To fetch the posts I have a query which includes several joins and is quite complicated to use through WP_Query.

SELECT p.post_name, t.name as sname, t2.name as cname FROM wpw0_posts AS p 
INNER JOIN wpw0_term_relationships AS tr ON p.ID = tr.object_id 
INNER JOIN wpw0_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id && tt.taxonomy = 'storylines' 
INNER JOIN wpw0_terms AS t ON t.term_id = tt.term_id 

INNER JOIN wpw0_term_relationships AS tr2 ON p.ID = tr2.object_id 
INNER JOIN wpw0_term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id && tt2.taxonomy = 'company' 
INNER JOIN wpw0_terms AS t2 ON t2.term_id = tt2.term_id 

WHERE p.post_status = 'publish' AND p.post_type = 'post' 
ORDER BY p.post_date DESC

So I fetched the posts using $wpdb object and get_results() method, but the original template has many WP_Query methods like get_the_post_thumbnail_url(), get_the_category(), etc.

So inorder to use the same template I want to convert the array of posts from

$wpdb->get_results($sql);

to

new WP_Query($args);

How can I convert the object or convert the query?