Snippets / PHP
Automatically get child CPTs location from parent CPT location field
// Hook for when events are submitted/updated
add_action( 'voxel/app-events/post-types/events/post:submitted', 'update_event_location' );
add_action( 'voxel/app-events/post-types/events/post:updated', 'update_event_location' );
// Hook for when places are updated
add_action( 'voxel/app-events/post-types/places/post:updated', 'update_related_events_location' );
// Hook for when places are submitted (only if necessary)
// add_action( 'voxel/app-events/post-types/places/post:submitted', 'update_related_events_location' );
function update_event_location( $event ) {
$post = $event->post;
// Ensure the post type is 'events'
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 ) {
// Retrieve and update the 'location' field if it exists
$location = json_decode( get_post_meta( $relation_id, 'location', true ), true );
if ( $location ) {
$post->get_field('location')->update( $location );
}
}
}
function update_related_events_location( $event ) {
global $wpdb;
$post = $event->post;
// Ensure the post type is 'places'
if ( $post->post_type->get_key() !== 'places' ) {
return;
}
$place_id = $post->get_id();
$location = json_decode( get_post_meta( $place_id, 'location', true ), true );
if ( ! $location ) {
return;
}
// Query the custom relations table to find events related to this place
$table_name = $wpdb->prefix . 'voxel_relations';
$event_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT child_id FROM {$table_name} WHERE parent_id = %d",
$place_id
) );
if ( empty( $event_ids ) ) {
return;
}
// Update location for each related event
foreach ( $event_ids as $event_id ) {
$voxel_event = \Voxel\Post::get( $event_id );
if ( $voxel_event && $voxel_event->post_type->get_key() === 'events' ) {
$voxel_event->get_field('location')->update( $location );
}
}
}
Discussion 1
Ask a question or share how you used this.
I'm trying to get this working on site, but am not sure what all I need to change to customize it (sorry - a novice at coding). If my post types are 'volunteer-profile' instead of 'events' and 'placements' instead of 'places', I would assume I would need to change it to what is below. I notice in the code there are calls for $event - does this need to be changed as well? // // Hook for when VOLUNTEERS are submitted/updated add_action( 'voxel/app-events/post-types/volunteer-profile/post:submitted', 'update_volunteer_location' ); add_action( 'voxel/app-events/post-types/volunteer-profile/post:updated', 'update_volunteer_location' ); // Hook for when PLACEMENTS are updated add_action( 'voxel/app-events/post-types/placements/post:updated', 'update_related_volunteer_location' ); // Hook for when PLACEMENTS are submitted (only if necessary) add_action( 'voxel/app-events/post-types/placements/post:submitted', 'update_related_volunteer_location' ); function update_volunteer_location( $event ) { $post = $event->post; // Ensure the post type is 'volunteer-profile' if ( $post->post_type->get_key() !== 'volunteer-profile' ) { 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 ) { // Retrieve and update the 'location' field if it exists $location = json_decode( get_post_meta( $relation_id, 'location', true ), true ); if ( $location ) { $post->get_field('location')->update( $location ); } } } function update_related_volunteer_location( $event ) { global $wpdb; $post = $event->post; // Ensure the post type is 'placements' if ( $post->post_type->get_key() !== 'placements' ) { return; } $place_id = $post->get_id(); $location = json_decode( get_post_meta( $place_id, 'location', true ), true ); if ( ! $location ) { return; } // Query the custom relations table to find VOLUNTEERS related to this PLACEMENT $table_name = $wpdb->prefix . 'voxel_relations'; $event_ids = $wpdb->get_col( $wpdb->prepare( "SELECT child_id FROM {$table_name} WHERE parent_id = %d", $place_id ) ); if ( empty( $event_ids ) ) { return; } // Update location for each related event foreach ( $event_ids as $event_id ) { $voxel_event = \Voxel\Post::get( $event_id ); if ( $voxel_event && $voxel_event->post_type->get_key() === 'volunteer-profile' ) { $voxel_event->get_field('location')->update( $location ); } } }