Support

Account

Home Forums General Issues How to update a value in a custom field by the post id

Helping

How to update a value in a custom field by the post id

  • Hi Elliot,

    I’m trying to make a custom field. The problem is that I can’t get it to save the values by post id. Now I’ve got a jquery slider, that can be moved up and down and sends a ‘minvalue’ and a ‘maxvalue’ with ajax. Now how would I catch these values so they are saved only by that post? Thought it was as easy as catching and returning the value in the update function with the ( $field_key, $value, $post_id) paramaters.

    Can you give a clue how to do this?

  • What action is your AJAX request calling? I’m guessing you’ll just need to pass the ‘minvalue’, ‘maxvalue’, and post ID (i.e. via $_POST) to some custom handler function, for example:

    
    function handle_slider_min_max() {
      $post_id = $_POST['post_id'];
      $min = $_POST['minvalue'];
      $max = $_POST['maxvalue'];
    
    update_field('slider_field_min', $min, $post_id);
    update_field('slider_field_max', $max, $post_id);
    
    }
    
    // user logged in
    add_action('wp_ajax_handle_slider_min_max', 'handle_slider_min_max');
    // user not logged in
    add_action('wp_ajax_nopriv_handle_slider_min_max', 'go_away');
    

    Note: you should use the field key in update_field(), rather than specifying it by name.

    Also, ‘go_away’ is not a native WP function (although it should be).

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

The topic ‘How to update a value in a custom field by the post id’ is closed to new replies.