Set Advert image to featured image automatically
-
Hi,
Great, great plugin. And also, thanks for the great documentation. I had some trouble setting the Advert image to featured post image so that I can grid the Adverts using Visual Composer on some pages. I came up with the following code;
// Custom Function: Get Attachment ID from URL function p_get_image_id($image_url) { global $wpdb; $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); return $attachment[0]; } // Custom Function: Add Auto Featured Image function auto_featured_image() { global $post; if (!has_post_thumbnail($post->ID)) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } else { if ( get_post_type($post->ID) == 'advert' ) { $image_url = "https://www.sinavlar.net/media/2016/11/classified-ni.jpg"; $attachment_id = p_get_image_id( $image_url ); set_post_thumbnail($post->ID, $attachment_id); } else if ( get_post_type($post->ID) == 'post' ) { $image_url = "https://www.sinavlar.net/media/2016/11/sinavlarnet-haber.jpg"; $attachment_id = p_get_image_id( $image_url ); set_post_thumbnail($post->ID, $attachment_id); } } } } // This was used to generate all featured images for old posts // add_action('the_post', 'auto_featured_image'); // Used for new posts add_action('save_post', 'auto_featured_image'); add_action('draft_to_publish', 'auto_featured_image'); add_action('new_to_publish', 'auto_featured_image'); add_action('pending_to_publish', 'auto_featured_image'); add_action('future_to_publish', 'auto_featured_image');
This works well when creating Adverts from the admin panel. However, I ran into problems when I tried to use the front-end posting form. Could you lend me a hand with this, thanks!
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Set Advert image to featured image automatically’ is closed to new replies.