Given the
$object_id
(and a WP_Term object), how does one go about
determining the$meta_type
to be passed toupdate_metadata()
?
Unfortunately, that is not possible because each object has its own table and therefore the same ID (1, 2, 3, etc.) could refer to a post, comment, term, etc.
So that’s why we have different functions which query different tables based on the object ID (that we pass to the function) and type (that is in the function name), e.g. get_post()
for retrieving a post object and get_comment()
for retrieving a comment object.
This can only be done by way of
update_metadata($meta_type, $object_id, $meta_key, $meta_value)
because I do not know the
$meta_type
(specifically, I cannot useupdate_post_meta()
,
update_comment_meta()
,update_user_meta()
, etc).
I don’t know, either. But looking at the WordPress database structure (and diagram like this), I think $object_id
should always be a post ID, therefore $meta_type
would then be post
. And here’s why:
-
Of all the core object types in WordPress, only posts that would have terms-taxonomy relationships (i.e. users for example, do not have taxonomies).
-
wp_set_object_terms()
inserts term-taxonomy relationship into thewp_term_relationships
table which (does not have anobject_type
field, and) has two primary keys —object_id
andterm_taxonomy_id
. So that means, the object IDs there should belong to the same object type.
PS: I copied the diagram/image above from here where at the time of writing (this answer), that article was last updated in August 2021. There’s also an official one on the (yes, outdated) Codex website..
So in summary, I don’t know how, but I might use if ( get_post( $object_id ) ) { copy the term meta to the post meta table }
..