Created
May 15, 2019 18:51
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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