0

I use plugin for submitting posts from frontend. It's a field builder. Maybe it uses wp_insert_post() function, I really don't know.

Here I use my custom shortcode, please check here for the code:

function my_custom_shortcode() {
    global $wpdb;
    $results = $wpdb->get_results( "SELECT city FROM slovak_cities" );
    
    $select = '<select name="city_field">';
    $select .= '<option value="-1">- select your city -</option>';
    
    foreach ( $results as $result ) {
        $select .= '<option value="' . str_replace( ' ', '_', strtolower( remove_accents( $result->city ) ) ) . '">' . $result->city . '</option>';
    }
    
    $select .= '</select>';
    
    return $select;
}
add_shortcode( 'my_shortcode', 'my_custom_shortcode' );

This shortcode creates a dropdown field in post form with many many options. All options inside are generated from database. This everything is OK and works good.

But I need help with saving this field as custom field to post when post form is submitted. The element has a name attribute named "city_field". This value should be a META KEY of custom field and META VALUE should be one selected option from element.

Can someone help me please? I know that this is related to add_post_meta() or update_post_meta() functions, but how to use them? Are there any action or filter hooks available?

2
  • you'll need to wrap it in a form tag and you'll need to add code that checks for the form submission and updates the post. You'll also want to check that the user can actually do this, and to check the post ID of the current post. Note that while you can update post meta with update_post_meta, you specifically said "field" which implies some sort of framework such as ACF. You will need to ask in an ACF community how to update an ACF field, or a WooCommerce community how to update a product attribute, or a metabox.io community how to... etc.. etc...
    – Tom J Nowell
    Commented Oct 5, 2022 at 12:28
  • What plugin are you using for submitting posts? You should check to see if it has documentation that you can hook into an action so you can get the post ID of the newly created object. With that, you can then use the update_post_meta function to save it.
    – AuRise
    Commented Oct 5, 2022 at 21:21

2 Answers 2

0

I you need to use update_post_meta($post_id,'your_meta_key','your_meta_value'); or you can save in new variable like $city_value = $_POST['city_field']; then code will be update_post_meta($post_id,'city_field',$city_value);

1
  • Yes, it's exactly as you say. I hooked it to two hooks wpuf_add_post_after_insert and wpuf_edit_post_after_update.
    – Juraj
    Commented Oct 8, 2022 at 20:19
0

I found two usable hooks wpuf_add_post_after_insert and wpuf_edit_post_after_update in the plugin documentation, that solved my problem:

function prefix_update_my_field( $post_id ) {
    if ( isset( $_POST['city_field'] ) ) {
        update_post_meta( $post_id, 'city_field', $_POST['city_field'] );
    }
}
add_action( 'wpuf_add_post_after_insert', 'prefix_update_my_field' );
add_action( 'wpuf_edit_post_after_update', 'prefix_update_my_field' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.