Support

Account

Home Forums Add-ons Repeater Field Querying a relationship field in a repeater field

Solved

Querying a relationship field in a repeater field

  • Hi,

    I have a custom post type ‘formation’. For each, I have a repeater field (‘chapitres’) with two sub-fields:
    • ‘chapitre’ which is a simple text field
    • ‘lecon’ which is a relationship field to classic wordpress post

    In the template of the formation custom post type, I’ve no problem to display each ‘chapitres’ with the ‘chapitre’ value and each posts in relation with.

    My problem is when I try to display the title of the formation post in a classic post.

    Here is my code :

    <?php while ( have_posts() ) : the_post(); ?>
       <?php 
       // filter
       function my_posts_where( $where ) {
    	
       $where = str_replace("meta_key = 'chapitres_%", "meta_key LIKE 'chapitres_%", $where);
    
      return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    /*
    *  Query posts for a relationship value.
    *  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    */
    
    $formations = get_posts(array(
    'post_type' => 'formation',
    'meta_query' => array(
    array(
    'key' => 'chapitres_%_lecon', // name of custom field
    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    'compare' => '='
    )
    )
    ));
    
    // query
    $the_query = new WP_Query( $formations ); ?>
    <?php if( $formations ): ?>
    							
    							
    <?php foreach( $formations as $formation ): ?>
    								
    <h6>
    <a href="<?php echo get_permalink( $formation->ID ); ?>">
    <?php echo get_the_title( $formation->ID ); ?>
    </a>
    </h6>
    
    <?php endforeach; ?>
    
    							
    <?php endif; ?>
    
    <?php endwhile; // end of the loop. ?>

    I’ve read all your tutorial and some thread on this forum but I did’nt find the solution for me.

    Thank you in advance for your help

  • Hi @jloupf

    I think you need to use LIKE for the compare value like this:

    'compare' => 'LIKE'

    I hope this helps 🙂

  • Hello @james

    Thank you for your answer.

    Unfortunaly, it does’nt work with ‘LIKE’ instead of ‘=’.

    When i do a var_dump($formations); I get an empty array array(0) { }

  • Hi @jloupf

    Could you please share the JSON export file of your field group so I can test it out on my installation?

    Thanks 🙂

  • Thank you James for your implication to solve this. I’m currently in vacation for 3 weeks without the possibility to send you the JSON file. I will send you the file when i will back.
    Have a nice day.

  • This reply has been marked as private.
  • Hi @jloupf

    I think I found the issue. It seems ‘post_where’ filter won’t work with get_posts() function. To fix it, you need to set suppress_filters option to false. Please take a look at the note on this page: https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where.

    So, your code should be something like this:

    $formations = get_posts(array(
        'suppress_filters' => FALSE,
        'post_type' => 'formation',
        'meta_query' => array(
            array(
                'key' => 'chapitres_%_lecon', // name of custom field
                'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
        )
    ));
    
    if( $formations ): ?>
    . . .

    I hope this helps 🙂

  • Hello @James,

    Thank you for your response.

    It’s work nice!

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

The topic ‘Querying a relationship field in a repeater field’ is closed to new replies.