This site is running Voxel Theme Version: 1.6.1.2 | Elementor Plugin Version: 3.32.3

Join the Voxel Guide Community!

Get Involved

Automatically get logo and featured image from related post

Description/Instructions

Automatically get the logo and the featured image from parent post relation field.
Just pay attention to post type (eg. “events”), logo (logo), featured image (_thumbnail_id) and post relation (post-relation) field keys.

Instructions

Get logo

add_action( 'voxel/app-events/post-types/events/post:submitted', 'update_events_logo' );
add_action( 'voxel/app-events/post-types/events/post:updated', 'update_events_logo' );

function update_events_logo( $event ) {
$post = $event->post;

// Check if it's a jobs post
if ( $post->post_type->get_key() !== 'events' ) {
return;
}

// Get the related post ID from the post-relation field
$relation_id = $post->get_field('post-relation')->get_value()[0] ?? null;

if ( $relation_id && is_numeric( $relation_id ) ) {
// Get the logo attachment ID from the related post
$logo_id = get_post_meta( $relation_id, 'logo', true );

if ( $logo_id && is_numeric( $logo_id ) ) {
// Update the events post's logo with the attachment ID
update_post_meta( $post->get_id(), 'logo', (int) $logo_id );
}
}
}

  • PHP
Copy Code

Instructions

Get featured image

add_action( 'voxel/app-events/post-types/events/post:submitted', 'update_events_featured_image' );
add_action( 'voxel/app-events/post-types/events/post:updated', 'update_events_featured_image' );
function update_events_featured_image( $event ) {
$post = $event->post;

// Check if it's an events post
if ( $post->post_type->get_key() !== 'events' ) {
return;
}

// Get the related post ID from the post-relation field
$relation_id = $post->get_field('post-relation')->get_value()[0] ?? null;

if ( $relation_id && is_numeric( $relation_id ) ) {
// Get the featured image attachment ID from the related post
$thumbnail_id = get_post_meta( $relation_id, '_thumbnail_id', true );

if ( $thumbnail_id && is_numeric( $thumbnail_id ) ) {
// Update the events post's featured image with the attachment ID
update_post_meta( $post->get_id(), '_thumbnail_id', (int) $thumbnail_id );
}
}
}

  • PHP
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24