Skip to content

Snippets / PHP

PHP

Automatically get logo and featured image from related post

G Gabi Maftei · 217 views · Updated 8 months ago
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.
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 · 24 lines
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 · 23 lines

Discussion

Ask a question or share how you used this.

Log in to join the discussion.

No comments yet — be the first.

Voxel Guide assistant
Ask about snippets, addons & how-tos

Hey — how can I help?

I can dig through the library for snippets, addons, and tutorials.

AI can be wrong — double-check anything important.