Transofrm sql query to wordpress query with new wp_query Transofrm sql query to wordpress query with new wp_query wordpress wordpress

Transofrm sql query to wordpress query with new wp_query


Try:

$wpq = new WP_Query();$wpq->parse_query($query);$posts = $wpq->get_posts();

or to use the standard WP_Query functions:

$wpq = new WP_Query($query);


I tried bobdye's solution, but it turns out it's not working. The only way, as far as I know to set up a loop where you can go and use the the_title(), $post->ID and so on, is to work like this:

global $post;$sql_results = $wpdb->get_results( $sql_query, OBJECT);if ($sql_results) {    foreach ($sql_results as $post) {      setup_postdata($post);      // You're in the loop now. You can access all the $post objects      // and functions like in any other wp loop.      // example: the_title();    }  wp_reset_postdata();}

A problem with this is pagination. But I believe this answer solves it neatly:https://wordpress.stackexchange.com/questions/21626/pagination-with-custom-sql-query#28717