Up to 50 percent off

Repeatable fields get messed up each time a post is updated

This topic contains 6 reply and 2 voices, and was last updated by cob-web 11 years, 2 months ago
Viewing 6 Posts - 1 through 6 (of 6 total)
Author Posts
August 28, 2013 at 10:03 pm 7586
cob-web Hi Tareq, I set up a repeatable field called 'credits'. When inserting credits, the actual order is very important (e.g director, camera, music etc). Each time I save a post, the order gets messed up. Also in the frontend the credits show in rather random order. The edit in this post doesn't make a difference to me: http://wedevs.com/support/topic/repeat-field-out-of-order/ I made some digging and found this http://wordpress.org/ideas/topic/add-order-by-meta_id-to-update_meta_cache but not sure if it has something to do with it. Do you have a hint? Thanks!
August 28, 2013 at 10:57 pm 7592
cob-web cob-web

I think I’ve found a solution for the frontend:


<?php $myid = $post->ID; 
                        $data = $wpdb->get_results("
                        SELECT * FROM $wpdb->postmeta WHERE post_id = $myid
                        AND meta_key NOT LIKE '\_%'
                        AND meta_key  IN('additional_credits')
                        ORDER by meta_id ASC
                    ");

                    if( !empty( $data ) ) {
                    foreach( $data as $result_object ) {
      
				$values = explode( '| ', $result_object->meta_value);
        $role_additional = "{$values[0]}";
        $url_additional = "{$values[2]}";
		if ($role_additional == '')
{ ?>
<?php } else { 
if ($url_additional  == '') // link to url when available
{ echo "{$values[0]}: {$values[1]} <br />"; } else { 
echo "{$values[0]}: <a target=\"blank\" href=\"$url_additional\">{$values[1]}</a><br />"; 
 	} 
  }
}}
?>	

But I still worry about the backend, as my client doesn’t like the idea of “random changing” 😉
Any idea on that would be very appreciated!

August 29, 2013 at 2:09 am 7601
cob-web cob-web

I just found out, that each time a post is updated the repeatable fields get saved for new! It means each time the meta_id of it increases! Is this normal? Is it the way it works? It’s at 17632 now and growing…. This actually kind of explains the problem with the reordering: when I create a group of ordered fields, each time I update the post, they get reordered and saved for new.
Is it a “normal” behaviour? 😉
Thanks in advance.

August 29, 2013 at 9:18 pm 7630
Tareq Hasan Tareq Hasan

Yeah, that was normal behavior. Because as you’ve said, get meta functions doesn’t provide you the meta ID’s. So when you are going to update those, there is no way to know if you are updating the same items or you are adding a new item. So the little trick was to delete all existing and adding the items again.

August 29, 2013 at 9:52 pm 7637
cob-web cob-web

Thanks Tareq!
One more question to this: is it also normal, that from time to time a new row of fields appears? I mean, I press “update” and suddenly there is an empty row of repeatable fields. I have to press the “-” to delete it. Have you experienced this?

August 29, 2013 at 11:58 pm 7641
Tareq Hasan Tareq Hasan

No, I haven’t faced that issue. May be I need to recheck.

August 30, 2013 at 12:04 am 7642
cob-web cob-web

All right then. I’m also using WPML, which doesn’t seem to be 100% compatible with your plug-in. I’ll keep an eye on that.
Thanks.

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