Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created November 11, 2014 01:05
Show Gist options
  • Save lgedeon/39d29dde00a11e3c2a0e to your computer and use it in GitHub Desktop.
Save lgedeon/39d29dde00a11e3c2a0e to your computer and use it in GitHub Desktop.
link two custom post types together using a taxonomy as a go between
/*===========================================================================
LINK EVENT LOCATION TAXONOMY AND POST TYPE
===========================================================================*/
function action__transition_post_status( $new_status, $old_status, $post ) {
// make sure we actually have an event object
if ( null === ( $post = get_post( $post ) ) || 'event_loc' !== $post->post_type ) {
return;
}
if ( in_array( $new_status, array( 'publish', 'draft', 'future') ) ) {
wp_create_term( $post->post_name, 'event_loc' );
} elseif ( $term_id = term_exists( $post->post_name, 'event_loc' ) ) {
wp_delete_term( $term_id, 'event_loc' );
}
}
add_action( 'transition_post_status', __NAMESPACE__ . '\action__transition_post_status', 10, 3 );
//todo change "name" of term if title of post changes
//todo handle slug changes too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment