Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Last active March 8, 2020 14:47
Show Gist options
  • Save everaldomatias/26fa5f8bbda8cda0e80cebd2532aaf4e to your computer and use it in GitHub Desktop.
Save everaldomatias/26fa5f8bbda8cda0e80cebd2532aaf4e to your computer and use it in GitHub Desktop.
if ( ! function_exists( 'auto_create_category' ) ) {
/**
*
* Create category with specific CPT is create or updated
*
* @author Everaldo Matias
* @link https://everaldo.dev
*
* @version 1.0
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*
* @see https://developer.wordpress.org/reference/hooks/save_post/
* @see https://developer.wordpress.org/reference/functions/wp_insert_term/
*
* @use replace 'example' with your cpt name
*
*/
function auto_create_category( $post_ID, $post, $update ) {
// Only set for post_type = example!
if ( 'example' !== $post->post_type ) {
return;
}
$title = get_the_title( $post_ID );
wp_insert_term( $title, 'category', ['slug' => $title] );
}
add_action( 'save_post', 'auto_create_category', 10, 3 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment