Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chrisvanpatten/da7e3c72e2a008ed8a1a4ddc2f0a8c4a to your computer and use it in GitHub Desktop.
/**
* Save term meta data.
*
* @param int $cat_id Category ID.
* @return void
*/
public static function create_category_page_data( $cat_id ) {
// Setup the values for the category page.
$author_id = get_current_user_id();
$post_type = 'category-page';
$post_status = 'draft';
$term = get_term( $cat_id, 'category' );
//Get parent category id
$parent_id = $term->parent;
//If the created category has no parent do nothing
if ( 0 !== $parent_id ) {
self::update_parent_category_page( $parent_id );
}
$post = [
'post_title' => sanitize_text_field( $term->name ),
'post_status' => $post_status,
'post_type' => $post_type,
'post_author' => $author_id,
'tax_input' => [
'category' => [
(int) $cat_id,
],
],
'meta_input' => [
'primary_category' => (int) $cat_id,
],
];
// Set the post ID so that we know the post was created successfully
if ( $post_id = wp_insert_post( $post ) ) {
if ( ! empty( $term->description ) ) {
update_post_meta( $post_id, 'dek', sanitize_text_field( $term->description ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment