Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active June 18, 2022 19:48
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
[WordPress] Programmatically Add Post Terms in WordPress
<?php
/**
* Appends the specified taxonomy term to the incoming post object. If
* the term doesn't already exist in the database, it will be created.
*
* @param WP_Post $post The post to which we're adding the taxonomy term.
* @param string $value The name of the taxonomy term
* @param string $taxonomy The name of the taxonomy.
* @access private
* @since 1.0.0
*/
private function set_post_term( $post, $value, $taxonomy ) {
$term = term_exists( $value, $taxonomy );
// If the taxonomy doesn't exist, then we create it
if ( 0 === $term || null === $term ) {
$term = wp_insert_term(
$value,
$taxonomy,
array(
'slug' => strtolower( str_ireplace( ' ', '-', $value ) )
)
);
}
// Then we can set the taxonomy
wp_set_post_terms( $post['ID'], $term, $taxonomy, true );
}
<?php
/**
* Determines if the given value has multiple terms by checking to see
* if a semicolon exists in the value.
*
* @param string $value The value to evaluate for multiple terms.
* @return bool True if there are multiple terms; otherwise, false.
*/
private function has_multiple_terms( $value ) {
return 0 < strpos( $value, ';' );
}
<?php
/**
* Loops through each of the multiple terms that exist and use the
* set_profile_terms function to apply each value to the given post
*
* @param WP_Post $post The post which we're applying the terms.
* @param string $values The delimited list of terms.
* @param string $taxonomy The taxonomy to which the terms belong.
*/
private function set_multiple_terms( $post, $values, $taxonomy ) {
$terms = explode( ';', $values );
foreach( $terms as $term ) {
$this->set_post_terms( $post, $term, $taxonomy );
}
}
<?php
/**
* Appends the specified taxonomy term to the incoming post object. If
* the term doesn't already exist in the database, it will be created. This
* function now supports multiple terms.
*
* @param WP_Post $post The current post with which we're working
* @param string $value The term value (or term name)
* @param string $taxonomy The name of the taxonomy to which the term belongs.
* @access private
* @since 1.0.0
*/
private function set_post_terms( $post, $value, $taxonomy ) {
/* First check to see if there are multiple terms and,
* if so, then loop through the values and update each
* term.
*/
if ( $this->has_multiple_terms( $value ) ) {
$this->set_multiple_terms( $post, $value, $taxonomy );
} else {
$term = term_exists( strtolower( $value ), $taxonomy );
// If the taxonomy doesn't exist, then we create it
if ( 0 === $term || null === $term ) {
$term = wp_insert_term(
$value,
$taxonomy,
array(
'slug' => strtolower( str_ireplace( ' ', '-', $value ) )
)
);
}
}
// Then we can set the taxonomy
wp_set_post_terms( $post['ID'], $term, $taxonomy, true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment