3

I use Wordpress 4.5 and I am trying to modify a query to use LEFT JOIN instead of INNER JOIN. But I cannot get 'relation' => 'OR' to work. See the query below. I have read a number of other posts about this but I cant get it right.

$premium_args = array(
        'meta_query' => array('relation' => 'OR', array( 'key' => 'premium', 'value' => '1', 'compare' => '=' )),
        'tax_query' => array( array( 'taxonomy' => 'posts-from', 'field' => 'id', 'terms' => $term->term_id ) ),
            'post_type' => 'post',
        'posts_per_page' => $posts_per_page,
        'meta_key' => 'verified',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'paged' => $paged
    );
    $premium = new WP_Query( $premium_args);

Below is the query result. How can I force it to use LEFT JOIN?

REQUEST:SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (7)
) AND ( 
  wp_postmeta.meta_key = 'verified' 
  AND 
  ( 
    ( mt1.meta_key = 'premium' AND CAST(mt1.meta_value AS CHAR) = '1' ) 
    OR 
    ( mt1.meta_key = 'verified' AND CAST(mt1.meta_value AS CHAR) = '1' )
  )
) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC LIMIT 0, 25
1

Wordpress is also provide more filters to change custom queries. https://codex.wordpress.org/Custom_Queries

add_filter('posts_join', 'custom_query_join' );
$premium = new WP_Query( $premium_args);
remove_filter('posts_join', 'custom_query_join' ); // remove filter bcz not effect to another query to use after this query.

function custom_query_join( $join ){
  $join .= 'Put here join to table';
  return $join;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.