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
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.