1 comment

  1. This should not be complicated so I can’t help but think that I am missing something. What it sounds like you need is get_post_meta.

    $yid = get_post_meta($post->ID,'youtube_id',true);
    if (empty($yid)) {
      // not set
    } else {
      // set
    }
    

    Edit:

    You need a meta_query for the most WordPress-y solution:

    $args = array(
      'post_type' => 'post',
      'meta_query' => array(
        array(
          'key' => 'youtube_id',
          'value' => 'some_value',
        )
      ),
      'ignore_sticky_posts' => true,
      'no_found_rows' => true,
      'posts_per_page' => 1,
      'fields' => 'ids',
    );
    $q = new WP_Query($args);
    var_dump($q);
    

    However, your code is easily adaptable:

    $different = $wpdb->get_var("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = 'youtube_id' AND meta_value = 'some_value'");
    if(!empty($different)) {
        echo 'Dude, we already have this post.';
    }
    

    Note: use $wpdb. It will save you some effort.

Comments are closed.