Use wp_update_post to update a field group

Support General Use wp_update_post to update a field groupResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #40568
    Macky McCormackMacky McCormack
    Participant

    Hi,

    I need to automatically update a field group after a custom post of a different type is created.

    I have tried this:

    
    add_action('save_post_default-tabs-info', 'fr_update_retreats_after_default_tabs', 10, 3);
    
    function fr_update_retreats_after_default_tabs {
    
      wp_update_post(array('ID' => 34));
    
    }
    
    

    Where the field group ID is 34, from the edit url: wp-admin/post.php?post=34&action=edit

    Is there a way to update the field group upon the save_post hook run?

    Thanks

    #40580
    PeterPeter
    Moderator

    Hello Macky,

    Which data do you need to update in the field group? A field group actually is a post that saves field and meta box settings. But it might be complicated if you want to update something in a field group.
    I think your code can trigger the function wp_update_post and work correctly.

    #40582
    Macky McCormackMacky McCormack
    Participant

    Hey Peter,

    I just need to updated the field group - equivalent to being in the edit screen for the field group and clicking 'Update'. This is to register changes that are made to a callback function that populated options in a select field.

    I have a similar set up for CPTs that works fine, but its not working for the field group.

    Thanks

    #40598
    PeterPeter
    Moderator

    Hello,

    I think the issue was resolved a long time ago, maybe related to https://support.metabox.io/topic/select-choices-callback-cache/

    Anyway, I test to add your code to my local site to update a field group title after saving a CPT post and it works correctly.

    add_action('save_post_my-job', 'fr_update_retreats_after_default_tabs', 10);
    
    function fr_update_retreats_after_default_tabs() {
        wp_update_post(
            array(
                'ID' => 3401,
                'post_title'   => 'This is the post title.',
            )
        );
    }

    And you can also use the filter hook rwmb_normalize_field to modify the field settings. Please read more on the documentation https://docs.metabox.io/filters/rwmb-normalize-field/

    #40602
    Macky McCormackMacky McCormack
    Participant

    Hi Peter,

    Yes I have seen the post that you're referencing - thats not a solution but it does outline the need update the field group after the callback function has been updated - which is exactly what I'm trying to do.

    RE rwmb_normalize_field - can you let me know who this will help, and how to use it in this case?

    Thanks

    #40615
    PeterPeter
    Moderator

    Hello,

    I think using the filter to set the options of the select field by coding instead of using the builder, might help you in this case.

    add_filter( 'rwmb_normalize_field', function( $field ) {
        //check your specific field before using this code
       $field['options'] = callback_function();
        return $field;
    } );
    #40629
    Macky McCormackMacky McCormack
    Participant

    Ah I see - I'll give that a try.

    Thanks

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