Ah, I found it. I just need to give a unique name to each term (so for the external URLs, I named them url-[md5]
where [md5]
is the MD5 hash of the URL), and then call add_term_meta
and set the necessary information, then add this term to the post using my custom taxonomies. For example, this is the working external term metadata setter:
foreach ( $external_urls as $url ) {
$url = esc_url( $url );
// don't use wp_hash here because it salts the data
$term_name = "url-" . md5( $url );
$terms[$term_name] = $url;
}
// update post's reference taxonomy terms (replaces any existing terms)
wp_set_post_terms( $post->ID, array_keys( $terms ), 'external_reference' );
// add metadata
foreach ( $terms as $term_name => $url ) {
// get term
$term = get_term_by( 'name', $term_name, 'external_reference' );
// add URL as metadata for the term created above
update_term_meta( $term->term_id, "url", $url );
}