Check the database for a postmeta field

Question

So I’m trying to use the metadata exists call to check the WordPress database and see if some metadata exists.

Here is the code:

function import_employees()
{
    $log = [];
    $start_script = false;

    $settings = get_settings();
    $octopus = get_octopus();

    /* Start the script at 0 */
    $settings['started'] = 0;

    /* If the started timestamp is less than or equal to finished, start the script */
    if ($settings['started'] <= $settings['finished']) {
        $start_script = true;
    }

    /* Grab all the posts from WP Database */
    $wp_query = new WP_Query([
        'post_type' => 'employee',
        'post_status' => 'any'
    ]);

    /* If start script is true, continue */
    if ($start_script) {

        /* Grab the first 500 records from Octopus */
        $records = $octopus->get_raw('employees');

        /* Returns all the Octopus records as an object */
        foreach ($records->data as $record) {

            if (metadata_exists('post', $wp_query, '_octopus_id')) {
                echo 'Hello!';
            }

            $args = array(
              'meta_key' => '_octopus_id',
              'meta_value' => $record->id,
            );
            var_dump($args);
        }
    }
}

var_dump($args) dumps the following loop:

array (size=2)
  'meta_key' => string '_octopus_id' (length=11)
  'meta_value' => int 10

I want to be able to check the database and if it doesn’t find _octopus_id, to echo ‘Hello’.

For some reason, I don’t think if the metadata_exists call is the correct one in this case, but I would like to check the database and see if an metadata of _octopus_id exists.

0
, , Sema 2 years 2019-11-01T15:30:05-05:00 0 Answers 104 views 0

Leave an answer

Browse
Browse