Custom Fields
Re-Envisioned
Support MB Frontend Submission How to assign a value to a taxonomy without showing it in the form in front-end?

  • Creator
    Topic
  • #21513
    Resolved EddyPiV
    Participant

    Hi,

    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?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Author
    Replies
  • #21521
    Long Nguyen
    Moderator

    Hi,

    You can use the action hook rwmb_frontend_after_save_post and the function wp_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/

    #21525
    EddyPiV
    Participant

    For 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?

    #21532
    Long Nguyen
    Moderator

    Hi,

    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-actions

    #21540
    EddyPiV
    Participant

    It’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?

    #21546
    Long Nguyen
    Moderator

    Hi,

    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 );

    You can add the code to the file functions.php in the child theme folder or use the plugin Code Snippets.

    #21586
    EddyPiV
    Participant

    Hi 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.

    #21590
    Long Nguyen
    Moderator

    Hi 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']
    
    
    #21596
    EddyPiV
    Participant

    What 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?

    #21610
    Long Nguyen
    Moderator

    Hi,

    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.

    #21621
    EddyPiV
    Participant

    Hi Long,

    Excellent, now it’s all working!

    Thanks so much for your help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.