By default, MyListing does not use the Featured Image portion of the Listing, so Listing owners cannot add their own Featured Image from the front end. However, third-party solutions for custom listing grids may require a featured image for the listing card’s media portion.
PHP
add_action( 'mylisting/submission/save-listing-data', function( $listing_id, $fields ){
$listing = \MyListing\Src\Listing::force_get( $listing_id );
if ( ! $listing ) {
return;
}
if ( ! $fields['job_gallery'] ) {
return;
}
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $fields['job_gallery']['value'][0] ));
if ( $attachment && is_array( $attachment ) && isset( $attachment[0] ) ) {
set_post_thumbnail( $listing_id, $attachment[0] );
}
}, 10e4, 2 );
add_action( 'mylisting/admin/save-listing-data', function( $listing_id, $listing ){
$listing = \MyListing\Src\Listing::force_get( $listing_id );
if ( ! $listing ) {
return;
}
$gallery = $listing->get_field( 'gallery' );
if ( $gallery && isset( $gallery[0] ) ) {
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $gallery[0] ) );
if ( $attachment && is_array( $attachment ) && isset( $attachment[0] ) ) {
set_post_thumbnail( $listing_id, $attachment[0] );
}
}
}, 10e4, 2 );