Sema
Sema

Reputation: 95

update_post_meta being used incorrectly

    /* This foreach is used to grab records from WordPress */
    foreach ($offices as $office) {

        /* Set the wordpress offices as an array */
        $id = get_post_meta($office->ID, '_office_id', true);

        /* If there are $ids, then set the $id as the key */
        if ($id != '') {
            $ids[$id] = $office;
        }
    }

    /* This foreach is used to grab records from an API */
    foreach ($records as $record) {
        /* Define the variable as the octopus id */
        $octopus_id = $record->id;

        /* Sets an variable to combine the location id and business unit */
        $record_key = strtoupper(str_replace(' ', '',
            trim($record->location_id) . trim($record->business_unit)));

        /* Checks to see if $ids is set or empty */
        if (isset($ids) & !empty($ids)) {

            /* Check if the record key exists in the ids array */
            if (array_key_exists($record_key, $ids)) {

                /* Check if an Octopus ID exists */
                if (!isset($octopus_id) || empty($octopus_id)) {
                    update_post_meta($ids, '_id', $record->id);
                } else {
                    echo $record . ' has not been updated.';
                }
            } else {
                echo '<b>' . $record_key . ' doesnt have a record in WordPress. The octopus id is: ' . $octopus_id . '</b><br/>';
            }
        }
    }

Hi all,

Could someone explain to me how I would be able to utilize the update_post_meta for if (!isset($octopus_id) || empty($octopus_id)) { if it's based out of another foreach? Kind of stuck on that part.

It's giving me an Expected int, got array error on the update_post_meta($ids, portion.

enter image description here

Upvotes: 1

Views: 130

Answers (1)

treyBake
treyBake

Reputation: 6560

You can see here that it expects an int(eger) instead of an array:

update_post_meta( int $post_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )

Updates a post meta field based on the given post ID.

You can either loop through your $ids array and execute it individually or instead of building the $ids array, execute there.

Upvotes: 2

Related Questions