Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created June 8, 2022 19:35
Show Gist options
  • Save petenelson/3ee704c47df21ebbdc8b39ff1f486b34 to your computer and use it in GitHub Desktop.
Save petenelson/3ee704c47df21ebbdc8b39ff1f486b34 to your computer and use it in GitHub Desktop.
WordPress: Add term to post
<?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