update_post_meta используется неправильно - PullRequest
0 голосов
/ 15 октября 2019
    /* 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/>';
            }
        }
    }

Привет всем,

Может ли кто-нибудь объяснить мне, как я смогу использовать update_post_meta для if (!isset($octopus_id) || empty($octopus_id)) {, если он основан на другом foreach? Вид застрял в этой части.

Это дает мне ошибку Expected int, got array на update_post_meta ($ ids, часть.

enter image description here

1 Ответ

1 голос
/ 15 октября 2019

Вы можете увидеть здесь , что он ожидает int (eger) вместо массива:

update_post_meta (int $ post_id, строка $ meta_key, mixed $ meta_value, mixed$ prev_value = '')

Обновляет мета-поле поста на основе заданного идентификатора поста.

Вы можете циклически проходить по массиву $ids и выполнять его отдельно или вместопостроив массив $ids, выполните там.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...