How to assign a value to a taxonomy without showing it in the form in front-end?
Support › MB Frontend Submission › How to assign a value to a taxonomy without showing it in the form in front-end?Resolved
- This topic has 10 replies, 2 voices, and was last updated 4 years, 7 months ago by
EddyPiV.
-
AuthorPosts
-
August 28, 2020 at 11:57 PM #21513
EddyPiV
ParticipantHi,
I have a form for a CPT, and I have a custom taxonomy (=location) assigned to that CPT, but the taxonomy is not part of the form. That is because each location has its own page showing the form in the front-end, and I do not want to bother the visitor selecting the location.
How to assign the value for that location to the taxonomy-field when the user submits a new custom post, so that at another webpage (with an archive view) all custom posts for that location can be shown?
August 29, 2020 at 12:57 PM #21521Long Nguyen
ModeratorHi,
You can use the action hook
rwmb_frontend_after_save_post
and the functionwp_set_object_terms()
to set the post terms after saving. Here is the sample code:add_action( 'rwmb_frontend_after_save_post', function( $post ) { wp_set_object_terms( $post->post_id, get_queried_object_id(), $taxonomy_slug ); } );
For more information, please follow these documents.
https://docs.metabox.io/extensions/mb-frontend-submission/#post-actions
https://developer.wordpress.org/reference/functions/wp_set_object_terms/August 29, 2020 at 7:35 PM #21525EddyPiV
ParticipantFor my understanding,
- $post->post_id must be the current post id,
- get_queried_object_id() is the id of the custom taxonomy to be set
- $taxonomy_slug is the value to be given to the taxonomy.
Right?How do I point here to the current post id?
For the rest, it should look like
add_action( 'rwmb_frontend_after_save_post', function( $post ) {
wp_set_object_terms( $post->post_id, 123, "Amsterdam" );
} );
where 123 is the id of the location taxonomy.
Is that correct?Where do I specify that action hook? Close to or with the [mb_frontend_form id="xxx"] command?
August 29, 2020 at 10:43 PM #21532Long Nguyen
ModeratorHi,
- get_queried_object_id() is a function get the term ID where the frontend form display. Don't change this function.
– $taxonomy_slug is the custom taxonomy slug, see my screenshot https://share.getcloudapp.com/Z4ukmlK0. In your case, it would belocation
.
You can check the meta box ID then set the post's term by using the action hook
rwmb_frontend_after_process
. Here is the sample code:add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { if ( 'xxx' === $config['id'] ) { wp_set_object_terms( $post_id, get_queried_object_id(), 'location' ); } }, 10, 2 );
For more information, please follow this documentation.
https://docs.metabox.io/extensions/mb-frontend-submission/#form-actionsAugust 30, 2020 at 7:19 PM #21540EddyPiV
ParticipantIt's obvious I am not a code writer...
I'm a bit puzzled: to set a value to the taxonomy should I use the rwmb_frontend_after_save_post or rwmb_frontend_after_process?
I think I understand your code
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
if ( 'xxx' === $config['id'] ) {
wp_set_object_terms( $post_id, get_queried_object_id(), 'location' );
}
}, 10, 2 );What I don't understand yet is:
- how to give a value ("Amsterdam") to the location
- how to give a value specific to the current page to the location, as I will have a page per location.
Does it take it from the current location of the page? (location taxonomy is also attached to pages).I have to set this php code in the functions.php file, right?
August 31, 2020 at 9:20 AM #21546Long Nguyen
ModeratorHi,
Reproduce your case:
- Create a custom post type, name Work Place.
- Create a custom taxonomy, name Location.
- Create some terms of Location: Amsterdam, Rotterdam, The Hague.
- Create 3 meta boxes: Amsterdam, Rotterdam, The Hague.
- Create a page then add the frontend shortcode.
[mb_frontend_form id='amsterdam' post_fields="title,content" post_type='work-place']
- Add the code to assign the location when submitting the post.
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { if ( 'amsterdam' === $config['id'] ) { wp_set_object_terms( $post_id, 'amsterdam', 'location' ); } }, 10, 2 );
- A screen record for this case https://www.loom.com/share/0ffb0fc49c164cc18ebf74774595cd45.
You can add the code to the file functions.php in the child theme folder or use the plugin Code Snippets.
September 1, 2020 at 9:27 PM #21586EddyPiV
ParticipantHi Long,
Back to my original question:
"I have a form for a CPT, and I have a custom taxonomy (=location) assigned to that CPT, but the taxonomy is not part of the form. That is because each location has its own page showing the form in the front-end, and I do not want to bother the visitor selecting the location.How to assign the value for that location to the taxonomy-field when the user submits a new custom post, so that at another webpage (with an archive view) all custom posts for that location can be shown?"
your approach forces me to have a metabox for each location, while I was working towards 1 metabox for the cpt, and the taxonomy location assigned to it, but not being part of the metabox. Isn't there a way to achieve it with 1 metabox for the cpt?
In your showcase, the term for the location is shown to the visitor, while I want it to be hidden.
So wouldn't a hidden field with the value for the location in each metabox be better?Anyway, when I follow each step of your example, I see the following happening:
- in the admin area I see both Amsterdam and Rotterdam as fields assigned to each post of cpt Work Place, see https://share.getcloudapp.com/WnuJm6kr.
Probably because I have set both metaboxes to cpt Work Place, but I assume it needs to be set there, right?
- but the location remains empty (https://share.getcloudapp.com/p9uGeZmZ), although I have copied your php code in the snippet (https://share.getcloudapp.com/2Nuybp9E) and included the snippet shortcode in the page (https://share.getcloudapp.com/2Nuybpog).
Hence it seems not to be working.September 1, 2020 at 11:16 PM #21590Long Nguyen
ModeratorHi Eddy,
We have to use many meta boxes along with the number of locations. WordPress doesn't know the location of the post to assign if you use only one meta box.
You can use the hidden field or other fields even leave the meta box blank with no field, it's just the demo.
Regarding the shortcode, please try to add the snippet code to the file functions.php in the theme folder or use Code Snippets and add the attribute post_type to the shortcode.
[mb_frontend_form id='amsterdam' post_fields="title,content" post_type='work-place']
September 2, 2020 at 5:28 PM #21596EddyPiV
ParticipantWhat I didn't mention is that the same (location) taxonomy is assigned to the page. I have a page for each location, with the [mb_frontend_form] command. My thought was that this [mb_frontend_form] can be the same on each page, and that the location can be derived from the taxonomy of the page.
Is there not a way to obtain the taxonomy value of the page and assign that to the taxonomy of the cpt?
That way, just one meta box would work for all locations.Back to your showcase with Work Place: still the location is not set. It remains empty.
Any advice?September 3, 2020 at 11:31 AM #21610Long Nguyen
ModeratorHi,
If you assign the taxonomy to the page, we can get the term of the page with the function get_the_terms() and remove the meta box id in the shortcode, only change the snippet code to this.
add_action( 'rwmb_frontend_after_save_post', function( $post ) { // Get the page ID $current_page_id = get_queried_object_id(); // Get list terms of the page, taxonomy "location" $term_ids = get_the_terms( $current_page_id, 'location' ); if( is_array( $term_ids ) ) { // Set first term for post type wp_set_object_terms( $post->post_id, $term_ids[0]->term_id, 'location' ); } } );
Screen record: https://www.loom.com/share/ac631d8123f945efb8f7ffc66f9c163b
If the location still does not set for the post type Work Place, please check the code is added to the file functions.php or use the plugin Code Snippets like my demo.
September 3, 2020 at 5:30 PM #21621EddyPiV
ParticipantHi Long,
Excellent, now it's all working!
Thanks so much for your help.
- get_queried_object_id() is a function get the term ID where the frontend form display. Don't change this function.
-
AuthorPosts
- You must be logged in to reply to this topic.