Skip to content

Instantly share code, notes, and snippets.

@polevaultweb
Last active August 13, 2020 22:01
Show Gist options
  • Save polevaultweb/01fa8aad1c4e4571b7513c08111aaca1 to your computer and use it in GitHub Desktop.
Save polevaultweb/01fa8aad1c4e4571b7513c08111aaca1 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'igp_image_post_taxonomy_terms', 'first_tag_to_category', 10, 4 );
function first_tag_to_category( $terms, $image, $post_id, $taxonomy ) {
$tags = isset( $image->tags ) ? maybe_unserialize( $image->tags ) : array();
if ( empty( $tags ) ) {
return $terms;
}
$first_tag = reset( $tags );
$term = get_term_by( 'name', $first_tag, $taxonomy );
if ( ! $term ) {
$term = wp_insert_term( ucfirst( $first_tag ), $taxonomy );
$term_id = $term['term_id'];
} else {
$term_id = $term->term_id;
}
$terms[0] = $term_id;
return $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment