WordPress.org

Ready to get started?Download WordPress

Forums

How to have several arguments in WP_QUERY Meta_Query (5 posts)

  1. groupewibi
    Member
    Posted 1 year ago #

    I would like to know what is the best way to use multiple argument in a WP_QUERY.

    I would like to display post Where ("rel_equip_visitor" or "rel_equip_host" LIKE $equipeid), AND (Data_match >= $today).

    Problem i have is do not know how to use (if it's possible) 2 arguments (AND and OR) in the same query, like we do in MySql.

    I have try severals thinks to get different type of results, but never the good one. I made some search, but it's seem not be possible. Is there is a way to make it working ?

    $args = array(
    'post_type' 	=> 	'matchs',
    'post_status' 	=>	'publish',
    'paged' => $paged,
    'meta_query' 	=> 	array(
    			'relation' => 'OR',
    			array('key' => 'rel_equip_visitor', 'value' =>  $equipeid),
    			array('key' => 'rel_equip_host', 'value' =>  $equipeid),
    				),
    			array('relation' => 'AND',
    			'meta_key' => 'date_match','meta_compare' => '>=','meta_value' => $today)
    );
    
    // The Query
    $query_matchs = new WP_Query( $args );
  2. elitereloaded
    Member
    Posted 1 year ago #

    You will have to build a custom query and use $wpdb->prepare and $wpdb->get_results OR modify the generated SQL using the different SQL filter hooks that WordPress provides for altering the Posts query.

  3. groupewibi
    Member
    Posted 1 year ago #

    Ok. Thank u.

    Im not very fimiliar with these way to process.
    I will have a check on both method.

    But i finding this meta_query, not very logic, or maibe i using it on a wrong way ?

  4. groupewibi
    Member
    Posted 1 year ago #

    I did my best, but i do not understand why it's retriveing only some value, and it's not ordering ;o(

    SELECT wp_posts.* FROM wp_posts 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_posts.post_type = 'matchs' AND (wp_posts.post_status = 'publish') 
    
    AND ( (wp_postmeta.meta_key = 'rel_equip_visitor' AND CAST(wp_postmeta.meta_value AS CHAR) = '201') OR (mt1.meta_key = 'rel_equip_host' AND CAST(mt1.meta_value AS CHAR) = '201') )  
    
    AND (wp_postmeta.meta_key = 'date_match' ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value ASC LIMIT 0, 10
  5. elitereloaded
    Member
    Posted 1 year ago #

    Each meta Key & Value pair needs their own table join.

    Let me know if you need further help.

Topic Closed

This topic has been closed to new replies.

About this Topic