Skip Navigation

[Gelöst] Save text field value to repeating field if it doesn’t already exist

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I have a Form that includes a custom field that accepts multiple values and another custom field that only accepts a single value. If the single value doesn't exist in the multiple value field, I would like to add it using cred_save_data.

Solution: You should get_post_meta on the repeating field, to access an array of values it currently stores. Something like this:

$itms = get_post_meta( $post_id, 'wpcf-all-items', false );

Now $itms is an array of instance values:

Array
(
    [0] => instance 1
    [1] => instance 2
    [2] => instance 3
)

Then you can use PHP's in_array function to check to see if the new value already exists. If not, add it. If so, skip adding it.

Relevant Documentation:
https://www.php.net/manual/en/function.in-array.php

This support ticket is created vor 5 Jahren, 3 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von davidS-53 vor 5 Jahren, 3 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1306655

I have two text fields: a regular one called "item", and a repeatable one called "all items".

When a user submits a form with a value in the "item" field, I need to add it to "wpcf-all-items", but only if it doesn't already exist in there.

I'm using this:

function sf_save_items($post_id, $form_data) {
	add_post_meta( $post_id, 'wpcf-all-items', $_POST["wpcf-item"], false);
}
add_action( "cred_save_data", "sf_save_items", 10, 2 );

Problem is, that keeps adding the same item over and over, rather than recognising it already exists. Changing to true doesn't seem to allow you to add anything.

Any ideas what I might be doing wrong?

#1307019

Hi, you should get_post_meta on the repeating field, to access an array of values it currently stores. Something like this:

$itms = get_post_meta( $post_id, 'wpcf-all-items', false );

Now $itms is an array of instance values:

Array
(
    [0] => instance 1
    [1] => instance 2
    [2] => instance 3
)

Then you can use PHP's in_array function to check to see if the new value already exists. If not, add it. If so, skip adding it. I use in_array in the template code below, or you can find more info here: https://www.php.net/manual/en/function.in-array.php

Also it's usually a good idea to add a form ID check inside this callback so you can apply this code to only some specific Form or Forms. Otherwise, it will be triggered every time any Form is submitted. Here's a template you can use:

add_action('cred_save_data', 'my_save_data_action',10, 2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    // add your code here  
  }
}

Replace the 12345 with a numeric Form ID, or a comma-separated list of numeric Form IDs if you want to apply the code to more than one Form.

#1310205

My issue is resolved now. Thank you!