Automate PDF image into custom post type.
-
Hi, Thank you for this plugin.
Is it possible to automatically set feautured image when i upload a pdf to a custom post type?
-
When a user uploads a file, The pdf image grnerator plugin generates a thumbnail of uploaded files. i want the generated thumbnail set as the featured image.
Hi.
I hope this function helps you.
//Automatically set PDF thumbnail as featured image. function save_pdf_thumb_as_featuredimage ( $post_id ) { if ( wp_is_post_revision( $post_id ) ) return; if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image $attaches = get_posts ( 'post_parent='.$post_id.'&numberposts=-1&post_type=attachment&post_mime_type=application/pdf&orderby=menu_order&order=ASC' ); if ( $attaches ): foreach( $attaches as $attach ): if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail update_post_meta( $post_id, '_thumbnail_id', $thumb_id ); break; } endforeach; endif; } add_action( 'save_post', 'save_pdf_thumb_as_featuredimage' );If you want always automatically to set first attachment as featured image even if it is PDF, try this function.
//Automatically set first image/PDF as featured image. function save_thumbnail_as_featuredimage ( $post_id ) { if ( wp_is_post_revision( $post_id ) ) return; if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image $args = array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'orderby' => 'menu_order date', 'order' => 'ASC ASC' ); $attaches = get_posts($args); if ( $attaches ): foreach( $attaches as $attach ): if ( $attach->post_mime_type == 'application/pdf' ){ if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail update_post_meta( $post_id, '_thumbnail_id', $thumb_id ); break; } } elseif ( preg_match("/^image\//", $attach->post_mime_type ) ) { update_post_meta( $post_id, '_thumbnail_id', $attach->ID ); break; } endforeach; endif; } add_action( 'save_post', 'save_thumbnail_as_featuredimage' );Thank you.
Thank you for answering me Mizuho. Where do i insert this function?
the functions.php in the theme which you are using.
Thank you for helping me Mizuho, I really appreciate it. I am using Easy digital Downloads bundled with Marketify as theme. So the custom post type is the “download” So when i am adding a download i insert the pdf from the media uploader. But the Featured Image wont be uploaded automatically? Why is that? I can provide you the webpage and more if you want. Please contact me on anonym97@hotmail.com
Thank you very much.
Take a look at This Mizuho
https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/post-types.php
Ok.
I’m not sure to be able to hook into EDD post type.
But it sounds like useful.
I’ll try in the near future.Thanks.
The topic ‘Automate PDF image into custom post type.’ is closed to new replies.