Support

Account

Home Forums Add-ons Repeater Field Simple update for repeater subfield

Solved

Simple update for repeater subfield

    • gauden

    • October 11, 2013 at 11:12 am

    Hello guys… I searched for the answer but can’t find anything similar and the update_field documentation is falling a little short for my limited knowledge…

    How do I update an specific subfield within a repeater…

    I already created 8 rows with 4 subfields each and I need to update the last item on each row… I just don’t get how to do it!!! please help me!! 😀

    Thanks!!! Here is my code:

    $guilds = get_field('guilds','options');
    foreach($guilds as $guild){
    	$members = get_users('meta_key=u_guild&meta_value='.$guild['guild_id']);
    	$guildXP = 0;
    	foreach($members as $member){
    		
    		$guildXP += $member->u_xp;
    		
    	}
    	$field_key = "guilds";
    	$post_id = "options";
    	$value = get_field($field_key, $guild['guild_id']);
    	$value[] = array("guild_xp" => $guildXP);
    	update_field( $field_key, $value, $post_id );
    }
    
    • Elliot

    • October 11, 2013 at 1:50 pm

    Hi @gauden

    The code you have looks to append a row to the guilds repeater. Is that correct?

    If you want to leave the amount of rows, but update 1 of the sub fields from each row, you need to change a lot of your code.

    Firstly, you need to loop through the $value array. This loop will give you all the rows. For each row, you need to update the sub field value.

    Then just update $value as you already have.

    Thanks
    E

    • gauden

    • October 11, 2013 at 1:53 pm

    Ok, Thanks Elliot… but the thing is I don’t know how to do that… how do I identify the exact subfield (yes, this code adds new rows and I don’t want that) I’m really not that advanced in coding… sorry… do you have an example you could help me with?

    • Elliot

    • October 11, 2013 at 1:58 pm

    Hi @gauden

    Sorry mate, that’s kind of outside the scope of this free support forum.

    Please do checkout all the available resources and ask about any issues you have, but please don’t ask for pre-written code.

    Thanks
    E

    • gauden

    • October 12, 2013 at 2:11 am

    Ok, first of all, sorry, I didn’t mean the last post to go like that (sounding for pre-written code)

    I had a nice sleep and re-read what I wrote as an answer and I had to be really tired to ask for that… I’ll be back with the solution! Thanks Elliot!

    • gauden

    • October 12, 2013 at 3:18 am

    Ok, so the finished code goes like this:

    $guilds = get_field('guilds','options');
    $g_counter = 0;
    foreach($guilds as $guild){
    	$members = get_users('meta_key=u_guild&meta_value='.$guild['guild_id']);
    	$guildXP = 0;
    	foreach($members as $member){ 
    		$guildXP += $member->u_xp;
    	}
    	$field_key = "guilds";
    	$post_id = "options";
    	$value = get_field($field_key, $post_id);
    	
    	$guild['guild_xp'] = $guildXP;
    	$value[$g_counter] = $guild;
    	
    	update_field( $field_key, $value, $post_id );
    	$g_counter++;   
    	?>
    	<tr>
    		<td><img src="<?php bloginfo('template_directory'); ?>/images/<?php echo $guild['guild_id']; ?>.png"></td>
    		<td class="guild-name"><?php echo $guild['guild_name']; ?></td>
    		<td class="skill-name"><?php echo $guild['guild_skill']; ?></td>
    		<td class="guild-xp"><?php echo $guild['guild_xp'];  ?></td>
    		<td class="guild-members"><?php echo count($members);  ?></td>
    	</tr>
    <?php } ?>

    I had to rewrite each row from the field and update the whole field every loop…

    And the trick is to use 2 variables for the same field… At least this is how I solved it 😀 if anyone has comments I’m open!

    Thanks for the tip Elliot 😀

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.