I cant see any new tag in the tag box, using
add_action( 'save_post', 'save_metadata_postoauto');
function save_metadata_postoauto($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if( empty($postid) ) return false;
if ( is_null($_REQUEST["postoauto"]) ) {
delete_post_meta($postid, 'postoauto');
} else {
update_post_meta($postid, 'postoauto', $_REQUEST['postoauto']);
}
if ( ''!= $_REQUEST['postoauto']) {
wp_set_post_tags( $postid, 'posto auto', true );
}
}
If the post meta is successfully updated, then $_REQUEST[‘postoauto’] has a valid value and thus wp_set_post_tags() should work. It does work on my test site, however I don’t have your meta box, so I needed to alter this part: if ( ''!= $_REQUEST['postoauto']) in order for wp_set_post_tags() to be called.
The only reason I can imagine why wp_set_post_tags() would not work is if there is some other code somewhere preventing it from working as expected.
You could at least confirm wp_set_post_tags() was called by adding this line immediately below:
error_log('Function wp_set_post_tags was called!');
Then alter the post meta value to initiate an update. Next check your error log for the related message.
It works, but I had to remove the old tag with the same name. If the old tag was selected, it didnt create another tag with the same name
I’m pleased it works! Not adding a duplicate tag is expected behavior. You’re responsible for correctly setting up a proper test environment 🙂
Ok. I have a question now. Is there a system to limit the tagging just to some options of the selector?
Yes, you could do something like:
if ( in_array( $_REQUEST['postoauto'], ['garage','covered','shared'])) {
wp_set_post_tags( $postid, 'posto auto', true );
}
You would list all the possible selection values in the array where the tag should be set. Or if it’s easier, all possible values where it would not be set and negate the in_array() return value with the ! operator.
I would like a variant of this function. It is possible to say: if a term of a custom taxonomy is selected, so print the tag “name of the tag” in the tags box ?