Assigning default field values for existing posts

Support General Assigning default field values for existing posts

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #44437
    SridharSridhar
    Participant

    When a custom field (text type to keep it simple) with a default value is created for a post type, say page, any new Pages created from that point onward will have that post meta with the default value when the Page is published/updated.

    But already existing Pages will not have the post meta unless we go in each and update them. This is fine for a few Pages but not ideal for larger sites.

    What is the recommended method for programmatically updating the post meta for existing Pages with the default field value?

    I tried writing the following in a function hooked to init that loops through Pages where the custom field in question does not exist

    $default_value = rwmb_get_field_settings( 'custom_title', '', $page->ID )['std'];
    
    rwmb_set_meta( $page->ID, 'page_title', $default_value );

    but it isn't working.

    #44442
    PeterPeter
    Moderator

    Hello Sridhar,

    If you use the function rwmb_set_meta(), you should hook to the action init with a priority later than 20. Or use the WordPress function update_post_meta() to set the field value for the page.

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