Support

Account

Home Forums Add-ons Repeater Field How to get a value from another repeater field?

Solving

How to get a value from another repeater field?

  • Hi everybody,

    I have another problem with my liveticker project.
    http://www.derschlag-handball.de/liveticker

    I have two repeater fields.
    1) The liveticker itself with each event (goals, etc.)
    2) A player-list where you can add their names an numbers

    Goal:
    I want to type only the number of the player who made a goal in order to save time. ACF should then search in the player list for the row where the number is located and put his name in the liveticker.

    I used the following code as a reference:

    
    <?php 
     
    // args
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'event',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'location',
    			'value' => '%Melbourne%',
    			'compare' => 'LIKE'
    		),
    		array(
    			'key' => 'location',
    			'value' => '%Sydney%',
    			'compare' => 'LIKE'
    		)
    	)
    );
     
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
     
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    

    And this is my modified code:
    `
    <?php
    $number = get_sub_field(‘liveticker_nummer’);
    $name = array(
    ‘numberposts’ => 1,
    ‘post_type’ => ‘liveticker’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘nummer_heim’,
    ‘value’ => $number,
    )
    )
    );
    $name_query = new WP_Query( $name );?>

    <?php if(get_sub_field(‘ereignis’) == “Tor”) : ?>
    durch <?php if( $name_query->have_posts() ) : while ( $name_query->have_posts() ) : $name_query->the_post(); ?>
    <?php the_field(‘name_heim’);?><?php endwhile; endif; ?><?php the_sub_field(‘liveticker_name’);?> (<?php the_sub_field(‘liveticker_nummer’);?>)</p>

  • Hi @kulturbanause

    Thanks for the code, however, you failed to mention any explanation about the code. What does it do? Where is it run? What does and doesn’t work? Have you debugged it? What are you expecting to happen? What does actually happen? Is there a line in particular that doesnt work? Are you unsure about the logic it needs to perform? Where did you get the code from originally? Is this copy / paste or have you written it all yourself?

    Thanks
    E

  • Hi @elliot

    sorry for that. so i’m going to explain it to you 🙂

    This is the code i took from your documentation (example 4):

    <?php 
     
    // args
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'event',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'location',
    			'value' => '%Melbourne%',
    			'compare' => 'LIKE'
    		),
    		array(
    			'key' => 'location',
    			'value' => '%Sydney%',
    			'compare' => 'LIKE'
    		)
    	)
    );
     
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endwhile; ?>
    	</ul>
    <?php endif; ?>
     
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

    And the following code is an extract of my entire liveticker code (http://derschlag-handball.de/liveticker). My liveticker works perfect so far, but it takes too much time to fill in all the fields when e.g. somebody makes a goal, etc.

    So what I did was to create another repeater field for each team, where I can write in all players with their numbers. When the match is running I only have to type in the number of the player and in my liveticker the name of the player will be picked from the new created repeater field.

    And there’s my problem. The liveticker repeater field works perfectly (where I always have to type in the players name and number). I also created a second (and third) repeater field for the team-list (number and name of each player). But my code with the modified query of your example (above) does not work:

    <?php
    $number = get_sub_field(‘liveticker_nummer’);
    $name = array(
    ‘numberposts’ => 1,
    ‘post_type’ => ‘liveticker’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘nummer_heim’,
    ‘value’ => $number,
    )
    )
    );
    $name_query = new WP_Query( $name );?>
    
    …
    
    <?php if(get_sub_field(‘ereignis’) == “Tor”) : ?>
    durch <?php if( $name_query->have_posts() ) : while ( $name_query->have_posts() ) : $name_query->the_post(); ?>
    <?php the_field(‘name_heim’);?><?php endwhile; endif; ?> (<?php the_sub_field(‘liveticker_nummer’);?>)</p>
    
    …

    get_sub_field(‘liveticker_nummer’) = get’s the number of the player I tiped in the liveticker repeater field (for each event, like e.g goal)

    ‘nummer_heim’ = the field of my team-list, where the number of the player is listed

    ‘value’ => $number = my goal is that the code uses the number in the liveticker repeater field to query the row of the team-list where also his name is located

    the_field(‘name_heim’) = the name which belongs to $number in the team-list

    I hope you understand what I mean and can help me with that! thanks 🙂

  • Hi @kulturbanause
    I think there are a few issues here:

    1. You use get_sub_field(‘liveticker_nummer’);. This would only work if you are within a has_sub_field loop. Is this correct?
    2. Your WP_Query is looking up sub field data (i think, but I’m a bit confused). If so, you need to use LIKE to allow for the wildcard row search.

    Perhaps you could attach some screenshots of the backend so I can clearly see the fields and how your code is attempting to interact with the data?

    Thanks
    E

  • 1. That’s right! I almost forgot about this….

    Here’s a link to a zip-file with two screenshots and the full php-code. I commented the area where the magic should happen:

    https://www.dropbox.com/s/e5dc0ttbxzc9roz/ACF_Problem.zip

    I hope you get through the code.. it contains many conditions :/

    Thanks a lot for your effort!

  • Hi @kulturbanause

    Your screenshots do not show enough information for me to understand the data relationship.

    Can you please include screenshots of the edit page which the data is input? Also, show screenshots of the field group including all fields and location rules.

    Attaching the full page of code is a bit overwhelming and I’m not sure If I can help you with this, as it’s not exactly a support question, but more of a ‘can you get this working for me’ type question.

    Have you done any debugging? Do you understand what line of code is the issue? Why is it an issue? What does your debugging tell you about this line of code?

    Thanks
    E

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

The topic ‘How to get a value from another repeater field?’ is closed to new replies.