Image taxonomy
Support › MB Term Meta › Image taxonomy
- This topic has 5 replies, 2 voices, and was last updated 3 years, 4 months ago by
vacdk.
-
AuthorPosts
-
November 30, 2021 at 7:01 PM #32286
vacdk
ParticipantHi
I use the plugin happyfiles, and have created some categories like Avatar
Then I have created some custom fields for users included an image field for an avatar images.So my problem is, when uploading the image through the frontend submission, the images goes to the default media library and is not assigned to any category by happyfiles "happyfiles_category" aka Avatar
I have contacted Thomas from happyfiles and his note was:
A HappyFiles folder is a custom taxonomy term in your database.
You could hook into the upload file event and assign any folder (= term_id) you like to it.
The HappyFiles taxonomy slug for files is: happyfiles_categorySo my question is, can this be done by the metabox custom field, ex may be in the advanced / Custom settings.
And if yes how ?
December 1, 2021 at 12:52 PM #32306Long Nguyen
ModeratorHi,
You can create a
taxonomy
ortaxonomy_advanced
in a field group (meta box) and assign the meta box to the post typeAttachment
. Then when editing an image uploaded, you can select and assign the term of the taxonomyhappyfiles_category
to that image. Screenshot https://share.getcloudapp.com/d5u65Nm8Please read more on the documentation
https://docs.metabox.io/fields/taxonomy/December 1, 2021 at 3:40 PM #32312vacdk
ParticipantThanks for the reply
The Avatar image is already hooked to the user profile, so it's not possible also to add it to the attachment, I know ACF can hook to multiple locations posts, pages, users but I don't see the option in MB.
As written in the earlier post, the Happyfiles plugin has already created the taxonomy.
I just want to set a taxonomy argument on the image upload. Hidden, as users shall not be able to select different options.I tried with JSON notation, but no luck.
CUSTOM SETTINGS
ex. tax_query = [{"happyfiles_category": "avatar", "term_id": "20", "slug": "avatar"}]December 1, 2021 at 10:19 PM #32325Long Nguyen
ModeratorHi,
The custom settings
tax_query
of a field is to filter the value and show it on the select field for the user to choose. It will not work in your case.I think you can use the Meta Box action hook
rwmb_{$field_id}_after_save_field
then set the term for the image ID (post type attachment). Refer to these documentations
https://docs.metabox.io/actions/#rwmb_after_save_field
https://developer.wordpress.org/reference/functions/wp_set_post_terms/December 2, 2021 at 4:41 PM #32357vacdk
ParticipantThanks Long
But the lack of documentation or examples I can not get it to work.
In this example, I am just trying to update a text field after saving.
Text field ID =text_plo7fflsjjp
add_action( 'rwmb_text_plo7fflsjjp_after_save_field', function ( $null, $field, $new, $old, $object_id ) { $settext = 'updated'; update( $field["id"], $settext , $new ); }, 10, 5 );
but i am getting a Fatal error: Uncaught Error: Call to undefined function update()
December 2, 2021 at 7:10 PM #32364vacdk
ParticipantSo, after some digging, I think I got it to work.
add_action( 'rwmb_galleri_upload_after_save_field', function ( $null, $field, $new, $old, $object_id ) { $args = array( 'post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => $object_id ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $attachment) { wp_set_object_terms( $attachment->ID, 'Galleri', 'happyfiles_category', false ); } } }, 10, 5 );
is this the right way to do it ? or is there an easier way ?
-
AuthorPosts
- You must be logged in to reply to this topic.