Check if image exists before uploading with media_sideload_image()
I wonder if you’re looking for the core attachment_url_to_postid() function that uses: $sql = $wpdb->prepare( “SELECT post_id FROM $wpdb->postmeta WHERE meta_key = ‘_wp_attached_file’ AND meta_value = %s”, $path ); $post_id = $wpdb->get_var( $sql ); Note the extra meta key _wp_attached_file check compared to your current snippet. If you need to check if an image url … Read more