-1

I've got 20+ hours into this and could really use some help. Here is what I am trying to do.. For each record, add post meta. If the post meta already exists, update each field in the array. Just like it would be with adding and updating a single custom field, except this one is repeatable based off of each record.

I know how to add post meta for each record. That's the simple part, it's adding it for each record and then updating it without adding any more before/after the update that I'm having a hard time with. Just for the sake of writing it out, here is just adding the repeating fields based off of each record.

foreach ($response->records as $record) {
  $sales = 'ten';
  update_repeating_meta('assigned-sales', $sales);
}

The function:

function update_repeating_meta($key, $value){
  add_post_meta($this->current_post_id, $key, $value);
}

That all works fine. Here is what I have so far for adding each one and then updating, that's not working:

foreach ($response->records as $record) {
  $sales = 'ten';
  update_repeating_meta('assigned-sales', $sales);
}

New function:

function update_repeating_meta($key, $value){
    $post_meta = get_post_meta($this->current_post_id, $key);
    if(!empty($post_meta)){
        foreach($post_meta as $key => $value) {
                update_post_meta($this->current_post_id, $key, $value);
        }
    }
    elseif(empty($post_meta)){
            add_post_meta($this->current_post_id, $key, $value);
    }
}

This code neither adds nor updates any of the results. Plus, I don't think it has support for updating the correct key/values in the post meta array after the meta data is added. I'm still trying my best to make it work and learn more about it. Sorry in advance if any of this doesn't make sense.

I'd really appreciate the help if anyone has any ideas.

0

Since you're using the same key for all the values you can simply use:

update_post_meta($this->current_post_id, 'assigned-sales', $response->records);

This will store the whole array in a single meta entry with key of 'assigned-sales'. WordPress will update the value if it exists and create new one otherwise, no need to have logic for that.

In case you insist on having separate meta key for each array entry you should add some sort of index to each meta key to make it unique (this is assuming you want each one and not just the latest value of the same).

I think this should do the trick:

foreach ($response->records as $key=>$record) {
  $sales = 'ten'; // <-- Where is this coming from?
  update_post_meta($this->current_post_id, 'assigned-sales-'.$key, $sales);
}

And here also the logic for add/update is redundant and you don't need a separate function.

It all depends on what you're after so if your issue persist, please clarify the question and what the use of the data is.

  • I do need to use repeating fields. The second code still doesn't add any post meta. How would the $key get any sort of value here? – IYBITWC Apr 7 '15 at 11:16
  • $key seems to be breaking my code. I think I need foreach ($response->records as $record). – IYBITWC Apr 7 '15 at 11:29
  • the $key=>$record is supposed to return an index in the absence of any real key in the array itself. You can also always do this: $i=0; foreach ($response->records as $record) { $sales = 'ten'; // <-- Where is this coming from? update_post_meta($this->current_post_id, 'assigned-sales-'.$i, $sales); $i++; } – td-lambda Apr 7 '15 at 16:16
0

If you use update_post_meta, a meta field will be added to the post with the specified key. If the key exists, the meta field will be updated. If there are several meta fields with the same key, all of them will be updated.

To add repeating fields, you must use add_post_meta with the third parameter set to false (it is already false as default). This way, it doesn't matter if the key exists or not, a new meta field will be added, so you can have several meta fields with the same key; that is, repeatable fields:

add_post_meta( $post->ID, 'mykey', 'value' );
add_post_meta( $post->ID, 'mykey', 'another_value' );

Now, if I use get_post_meta for mykey, I get two entries:

// $meta_values will be an array with all entries for 'mykey'
$meta_values = get_post_meta( $post->ID, 'mykey' );

// print "value"
echo $meta_values[0];

// print "another_value"
echo $meta_values[1];

If you need to update a specific entry for 'mykey', you have to specify the old value you want to update. This way, the meta field to be updated can be differentiated from other meta fields with the same key:

// only the entries for mykey with value "another_value" will be updated to "new_value"
update_post_meta( $post->ID, 'mykey', 'new_value', 'another_value' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.