To get the URL of the uploaded image/attachment, you can use wp_get_attachment_url()
(which always returns the full-sized image URL) or wp_get_attachment_image_url()
for image attachment:
// This is how you should call uploadImageToMediaLibrary(); assign the value to $att_id.
$att_id = uploadImageToMediaLibrary($post->ID, $external_url, "custom_alt");
$url = wp_get_attachment_image_url( $att_id, 'full' ); // full-sized URL
But to set the image as the featured image of the post where the image was uploaded to, or any posts actually, then you would use set_post_thumbnail()
like so:
$att_id = uploadImageToMediaLibrary($post->ID, $external_url, "custom_alt");
if ( ! is_wp_error( $att_id ) && $att_id ) {
set_post_thumbnail( $post->ID, $att_id );
}
PS: I revised this answer because using set_post_thumbnail()
is much preferred than manually updating the private metadata _thumbnail_id
used for post featured images.. =)
Related Posts:
- Creating a metabox to upload multiple images, Ignoring The Featured Image
- How to hide CPT files from media library programmatically
- Add metabox with media uploader in a custom post type [duplicate]
- How to Removing fields from the Media Uploader/Gallery on a Custom Post Type Edit Page
- get_terms won’t display product_cat or any other custom taxonomies when specified
- Remove fields on media uploader for custom page type – not working for “From Computer” or “From Url” tabs?
- Duplicate Custom Header Functionality into the post edit screen
- Redirect to another page using contact form 7? [closed]
- File sharing platform for user contributions? [closed]
- Cannot upload featured image to a custom post type
- How to manage a bookstore
- Issue On Listing Woocommerce Parent Tag List
- get_terms by custom post type
- Get terms by taxonomy AND post_type
- Get Posts by Custom Post Type ,Taxonomy, and Term
- Get The Post Type A Taxonomy Is Attached To
- Submit post and upload image from front-end
- How to disable generation of default image sizes for some custom post types?
- Media library – Limit images to custom post type
- How can I bulk upload images and automatically create posts for each one at the same time?
- Use a separate upload folder for custom post attachment upload
- Display current taxonomy term when inside custom post type
- Handling front-end file uploads, considering safety and ease of use
- Get term slug of current post
- Hide “Add media”, HTML editor from TinyMCE
- How to limit the number of terms (terms acts like categories)
- How do I list custom taxonomy terms without the links?
- query multiple taxonomies
- wp_set_object_terms() is not replacing term, but creating a new one
- Get terms that contain posts that in turn belong to other terms?
- wp_insert_term doesn’t work with custom post type’s taxonomy
- Set custom upload path for custom post type only?
- show tags of custom post type ONLY
- Custom Taxonomies Terms as Post Title for Custom Post Types upon Publishing
- How to get term link that crosses different custom post types?
- wp_get_object_terms(): count relative to passed IDs?
- Attach a Custom Post Type to a Woocommerc product as a variation or add-on [closed]
- Count posts that have specific taxonomy term attached
- Retrieve single term slug
- Remove tabs from media uploader for a CPT
- Exclude a term of a taxonomy with a custom post type in a search
- Not Able to Insert Taxonomy Term Using wp_insert_post()
- array_pop() expects array, boolean given for $event_type_term ->slug
- How do I add media to a custom post type?
- How can I add programmatically custom taxonomy terms to a custom type post when saving posts?
- post count is wrong when using same taxonomy for 2 different CPT
- How to hook get_terms() to only show count of posts that have custom meta
- display multiple term post from taxonomy in a single page
- Glossary with Custom Post Type
- Custom Post Type Archive Template
- Removing Image Sizes for Custom Post Type
- Maintaining strict one-to-one association between terms and custom posts
- Displaying custom post type by first letter through custom taxonomy
- Retrieve a specific field from taxonomy term through advanced custom fields [closed]
- If on term-page -> get the current term?
- How to solve suspected memory issue in custom WordPress loop?
- Separate Custom Post Type Taxonomy by Comma
- Adding a term name from a custom taxonomy assigned to a post link displayed by a wp_query loop based on another taxonomy
- Image size filtering in Media uploader according to custom post type
- Custom Search | Wrong output & question
- Sort the main query in subcategories/terms?
- Rename the insert button in media upload window
- Trouble adding custom featured image for custom taxonomy TERMS
- Get child terms of current term of the current post
- Query for posts from any post type but only add instock products
- Get list of terms of current taxonomy archive for another taxonomy
- WooCommerce – Show orders for a specific product ? [closed]
- Change upload_dir folder at a certain cpt but cant change back
- List all custom post type posts from a given category?
- Creating a function that receives the taxonomy terms that have been changed in a custom post type
- Limit upload by file type only for certain custom post type
- Output the slug and name of a CPT single post taxonomy term
- Get all taxonomies for all post types
- Get the terms of a custom taxonomy for a specific author in author template
- wp is not defined error using wp.media to create a custom image uploader
- Dynamically create/remove terms in taxonomy when custom post type is published/trashed
- Set meta_query only for specific post type
- How do I organize photos for multiple galleries?
- How to get the first term for the current taxonomy?
- How do I provide a “show all posts” link in a paginated term archive?
- How to get category image custom post type taxonomy in wordpress?
- Upload Image from Front End and Resize
- Category page only displaying the posts from a custom type
- Delete all associated media when custom post type removed
- MP3 Manager for WordPress
- Woocommerce query specific product from specific category
- Output the content of a term organised by each of the CPT?
- How to rename image at uploading on specific plugin or post-type in WordPress
- How to filter the taxonomy terms based on another taxonomy term
- Problem adding column to WC Subscription [closed]
- How to get posts from multiple custom post types according to it’s custom taxonomy terms?
- How to output wordpress custom tags separated by comma?
- Have a Custom Post Type index page display taxonomy items instead of posts
- How to check the terms in single custom post type template
- Cannot attach media when capabilities added to custom post type
- Create custom post type on successful woocommerce order [closed]
- window.send_to_editor and jQuery .attr() conflicts with multiple custom upload image meta boxes
- Displaying a custom post types custom taxonomy value?
- get_the_terms return only last term
- Does WordPress limit the length of slug names for Post Meta or Terms?