Reputation: 114
I'm trying to use author meta data to automatically populate a custom taxonomy on a given author's post(s). Currently, the taxonomy is visible on the post page, but I intend on hiding it from the front end to avoid accidental user selections. (Also the reason for pre-population)
Note: The associated taxonomy terms and the author meta data are identical (the author meta data options are pulled dynamically from existing taxonomy terms).
For some reason - this is NOT working. I'm scratching my head trying to figure out why - I think the logic is correct. Am I missing something/can anyone help?
Thanks in advance.
function update_school($post_id) {
$post_author_id = get_post_field( 'post_author', $post_id ); // get the post author ID
$school_title = get_the_author_meta( 'school2', $post_author_id ); // from the post author ID, get the author meta
$school_slug = get_the_author_meta( 'school', $post_author_id ); // from the post author ID, get the author meta
$school_term = get_term_by('slug', $school_slug, 'school'); // get term by the author meta called "school"
$school_term_name = $school_term->name; // from the slug, get the school term name
// update the post taxonomy "school" with author meta school variables above
wp_set_post_terms( $post_id, $school_term_name, 'school' );
}
// run function on post save
add_action('save_post', 'update_school');
Upvotes: 0
Views: 186
Reputation: 114
Figured it out for anyone who wants to know:
function update_school( $post_id ) {
$post_author_id = get_post_field( 'post_author', $post_id ); // get the post author ID
$school_name = get_the_author_meta( 'school2', $post_author_id ); // from the post author ID, get the author meta
$term = term_exists( $school_name, 'school');
wp_set_post_terms( $post_id, $term, 'school' );
}
// run function on post save
add_action( 'save_post', 'update_school' );
Upvotes: 0