Over on LezWatch TV we have two post types, shows and characters. In an added wrinkle, there’s data that exists on the show pages that is updated by the character pages.

That means when I save a character page, I need it to trigger a series of updates to post meta of whatever shows the character belongs to. But because of the complexities of the data being saved, I need to run it when the show saves too.

The Code

add_action( 'do_update_show_meta', 'lezwatch_update_show_meta', 10, 2 );
add_action( 'save_post_post_type_shows', 'lezwatch_update_show_meta', 10, 3 );
add_action( 'save_post_post_type_characters', 'lezwatch_update_show_meta_from_chars', 10, 3 );

function lezwatch_update_show_meta( $post_id ) {

	// unhook this function so it doesn't loop infinitely
	remove_action( 'save_post_post_type_shows', 'lezwatch_update_show_meta' );

	[Update all the post meta as needed here]

	// re-hook this function
	add_action( 'save_post_post_type_shows', array( $this, 'lezwatch_update_show_meta' ) );

}

function lezwatch_update_show_meta_from_chars( $post_id ) {
	$character_show_IDs = get_post_meta( $post_id, 'lezchars_show_group', true );
	if ( $character_show_IDs !== '' ) {
		foreach ( $character_show_IDs as $each_show ) {
			do_action( 'do_update_show_meta' , $each_show['show'] );
		}
	}
}

This runs three actions, saving shows and characters, and also a custom one that the character page will call. It needed to be split like that because some characters have multiple shows.

Reader Interactions

%d bloggers like this: