Use wp_update_post to update a field group
- This topic has 6 replies, 2 voices, and was last updated 2 years, 1 month ago by
Macky McCormack.
-
AuthorPosts
-
February 17, 2023 at 12:00 AM #40568
Macky McCormack
ParticipantHi,
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
February 17, 2023 at 10:41 PM #40580Peter
ModeratorHello 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.February 17, 2023 at 11:09 PM #40582Macky McCormack
ParticipantHey 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
February 18, 2023 at 11:50 PM #40598Peter
ModeratorHello,
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/February 19, 2023 at 12:53 AM #40602Macky McCormack
ParticipantHi 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
February 20, 2023 at 7:28 PM #40615Peter
ModeratorHello,
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; } );
February 21, 2023 at 12:18 AM #40629Macky McCormack
ParticipantAh I see - I'll give that a try.
Thanks
-
AuthorPosts
- You must be logged in to reply to this topic.