Support

Account

Home Forums General Issues Query with Relationship field

Solving

Query with Relationship field

  • Hi,

    I’ve a tourism website with 2 custom post type :
    “Offre” (Holiday deals) and “Proposition” (Proposals for travelers)

    A “Proposition” has some fields, one of them is a relationship field called ‘offre_liee’ which is linked to a post type “Offre”

    I want to display only one “Proposition” which is linked to a specific “Offre” with the ID of it.

    In a “Proposition” post, the field linked to an “Offre” is named “offre_liee”.

    Here is the code :

    
    $offre=get_post(21969);
    $offre_id=$offre->ID;
    var_dump($offre_id);
    
    $favoris = get_posts(array(
    			'post_type' => 'proposition',
    			'numberposts' => -1,
    			'meta_query' => array(
    						array(
    							'key' => 'offre_liee', 
    							'value' => $offre_id, 
    							'compare' => '='
    						)
    					)
    					
    				));
    
    var_dump($favoris);
    
    

    The first vardump works fine, it returns : the ID of the “Offre” : int(21969)

    And unfortunately, the “var_dump($favoris);” return no result : array(0) { }

    In the backoffice I can see that the “Offre” is properly linked to the “Proposition” with a Relation type field .
    In ACF settings, I’ve tried with two options : “Term Object” and “Term ID”.

    Anyone understand why the query return no result ?

    Thank you for your help

  • Hi @mclambert

    That’s because the relationship field saves the data in the database as a serialized IDs. If you want to query it, I believe you should do it like this:

    'meta_query' => array(
        array(
            'key' => 'offre_liee', 
            'value' => '"' . $offre_id . '"', 
            'compare' => 'LIKE'
        )
    ),

    This page should give you more idea about it: https://www.advancedcustomfields.com/resources/querying-relationship-fields/.

    I hope this helps 🙂

  • Hi James

    Thank you for your help, it works !

    I’ve tried with an array of ID, it doesn’t work, it returns an empty array.

    Here is what I’ve done, unfortunately it doesn’t work…
    Do you have an idea ?

    Thank you very much for your time.

    <?php 
    
    $preargs = array(
      'post_type'  => 'offres'
     );
    
    $the_query = new WP_Query( $preargs ); 
    $offres_id = array(); //added
    
    while ( $the_query->have_posts() ) : $the_query->the_post();
     $offres_id[] = get_the_ID();
    endwhile; 
    
    // $arraydeidoffre RETURNS : array(7) { [0]=> int(25747) [1]=> int(25356) [2]=> int(24140) [3]=> int(23097) [4]=> int(23054) [5]=> int(22286) [6]=> int(12005) } 
    
    $propositions = get_posts(array(
         'post_type' => 'proposition',
         'numberposts' => -1,
         'meta_query' => array(
          array(
           'key' => 'offre_liee', 
           'value' => $offres_id, 
           'compare' => 'IN'
          )
         )
    
        ));
    
    var_dump($propositions);
    
    ?>
  • Hi @mclambert

    I’m afraid you need to use multiple ‘meta_query’ values like this:

    $meta_query = array('relation' => 'AND');
    foreach( $offres_id as $the_id ) {
        $meta_query[] = array(
            'key' => 'offre_liee', 
            'value' => '"' . $the_id . '"', 
            'compare' => 'LIKE'
        );
    }

    Then you can use it like this:

    'meta_query' => $meta_query,

    This will create the query for all of the IDs you get. Unfortunately, I’m not sure regarding the performance if you have a lot of IDs.

    I hope this helps 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Query with Relationship field’ is closed to new replies.