Support

Account

Home Forums Front-end Issues Filing taxonomies using front-end submissions

Solved

Filing taxonomies using front-end submissions

  • I am writing a simple front-end submission script but having difficulty getting the front-end generated post to file in a taxonomy.

    It works perfectly if a user is logged in, but not for a general member of the public who isn’t logged in at the time of submission. Looks like a permissions issue according to this post. The difference here being the taxonomy terms already exist, I don’t want to create any new ones; I just need the submitted content to be filed according to the user’s submitted data.

    For the avoidance of doubt, the post is still created as intended when the user isn’t logged in, it just isn’t filing in any taxonomies at all.

    Here is the code I have:

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
    
        $post = array(
            'post_status' => 'publish',
            'post_type'   => 'application'
        );
    
        // Some logic goes here to determine what taxonomy term the post is to be filed in
    
        $post += ['tax_input' => array(
            'taxonomy1_to_use' => $id_of_taxonomy_term1,
            'taxonomy2_to_use' => $id_of_taxonomy_term2
        )];
    
        // insert the post
        $post_id = wp_insert_post( $post );
        
        // return the new ID
        return $post_id;
    
    };
    acf_form_head();

    That’s it. Like I say, it works perfectly if a user is logged in. But no taxonomies are present if submitted by a non-logged-in user.

    Any help with this matter will be most appreciated.

  • Hi @jack1

    Could you please try to use the acf/save_post hook instead? This page should give you more idea about it: https://www.advancedcustomfields.com/resources/acfsave_post/.

    So, your code should be something like this:

    add_action('acf/save_post' , 'my_acf_save_post', 20 );
    function my_acf_save_post( $post_id ) {
        
        // only do this for application post type
        if( !get_post_type($post_id) == 'application' ) return;
        
        // initialize the post array
        $post = array(
            'ID'          => $post_id,
        );
    
        // Some logic goes here to determine what taxonomy term the post is to be filed in
    
        $post += ['tax_input' => array(
            'taxonomy1_to_use' => $id_of_taxonomy_term1,
            'taxonomy2_to_use' => $id_of_taxonomy_term2
        )];
        
        // remove the hook to avoid the infinite loop issue
        // https://codex.wordpress.org/Function_Reference/wp_update_post#Caution_-_Infinite_loop
        add_action('acf/save_post' , 'my_acf_save_post', 20 );
    
        // insert the post
        wp_update_post( $post );
        
        // add the hook back
        add_action('acf/save_post' , 'my_acf_save_post', 20 );
    
    };

    I hope this helps 🙂

  • Including this hook now redirects the front-end visitor to the wordpress login form (?)

    As far as I can see from the documentation, it’s the acf/pre_save_post hook I need as this was made with front-end submissions in mind.

    Just can’t get it to save the taxonomies!

    It worked on a previous version of WordPress (4.5.4), but now it doesn’t since I upgraded to 4.7.

    Any more suggestions?

  • Hi @jack1

    I’m sorry I thought you were using the PRO version.

    I’ve just checked your issue, and it seems this issue is related to WordPress. This page should give you more idea about it: http://wordpress.stackexchange.com/questions/99405/wp-insert-post-not-updating-custom-taxonomy-selected-if-logged-in-as-a-subscribe.

    I hope this helps 🙂

  • Thanks for that James. Massively appreciated. A WordPress issue after all!

    I have used the wp_set_object_terms() function instead and now it’s working.

    Great stuff. Many thanks once again for your guidance.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Filing taxonomies using front-end submissions’ is closed to new replies.