Created
June 8, 2022 19:35
-
-
Save petenelson/3ee704c47df21ebbdc8b39ff1f486b34 to your computer and use it in GitHub Desktop.
WordPress: Add term to post
This file contains hidden or 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
<?php | |
$term = 'Some Term Name' | |
$taxonomy = 'some-taxonomy'; | |
if ( function_exists( '\wpcom_vip_term_exists' ) ) { | |
$term_data = \wpcom_vip_term_exists( $term, $taxonomy ); | |
} else { | |
$term_data = term_exists( $term, $taxonomy ); // phpcs:ignore | |
} | |
if ( ! is_array( $term_data ) ) { | |
$term_data = wp_insert_term( $term, $taxonomy ); | |
} | |
wp_set_post_terms( $post_id, $term_data['term_id'], $taxonomy ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment