Support

Account

Home Forums Backend Issues (wp-admin) Check if file id is attached to post Reply To: Check if file id is attached to post

  • If you have the file ID you can either do a query of posts using WP_Query for posts that belong to the user and a meta_query to look for the attachment id in in the field where it is added.

    This is only an example, you’ll need to alter to your values and test

    
    $user_id = get_current_user_id();
    $args = array(
      'post_type' => 'the-post-type',
      'author' => $user_id,
      'posts_per_page' => 1, // only need to see if one exists
      'meta_query' => array(
        array(
          'key' => 'custom_field_name',
          'value' => $file_id
        )
      )
    );
    $query = new WP_Query($args);
    if (count($query->posts)) {
      // this file is attached to at least one post by this user
    }