Skip to content

Instantly share code, notes, and snippets.

Created August 4, 2010 00:14
Show Gist options
  • Save anonymous/507429 to your computer and use it in GitHub Desktop.
Save anonymous/507429 to your computer and use it in GitHub Desktop.
<?php
/**
* @package Custom post type relation test
* @version 0.1
*/
/*
Plugin Name: Custom post type relation test
Plugin URI: http://wordpress.org/#
Description: Custom post type relation test
Author: Laurent Dinclaux <lox.dev@knc.nc>
Version: 0.1
Author URI: http://www.knc.nc
*/
function cptr_init() {
/* registers post type */
$args = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
$args->label = 'Movie';
$args->public = true;
$args->supports = explode(',', 'title,editor,excerpt,page-attributes');
register_post_type( 'movie', (array)$args );
$args->label = 'Actor';
register_post_type( 'actor', (array)$args );
/* registers taxonomy */
$args = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
$args->label = 'Actors';
register_taxonomy('actors',array('movie', 'actor'), (array)$args );
}
add_action( 'init', 'cptr_init', 0 );
function cptr_post_updated ($post_ID, $post, $post_before) {
if($post->post_type !== 'actor') return;
// status changes
if($post->post_status !== $post_before->post_status) {
if($post->post_status === 'publish') {
wp_set_object_terms($post->ID, $post->post_title, 'actors');
}
else {
if($term = get_term_by( 'name', $post->post_title, 'actors' ) ) {
wp_delete_term($term->term_id, 'actors');
}
}
}
elseif($post->post_status === 'publish') {
if( $post->post_title !== $post_before->post_title
|| $post->post_name !== $post_before->post_name) {
$term = get_term_by( 'name', $post_before->post_title, 'actors' );
if( $term ) {
wp_update_term($term->term_id, 'actors', array('slug'=>$post->post_name, 'name'=>$post->post_title));
}
}
}
}
add_action('post_updated', 'cptr_post_updated',10 ,3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment