How to dynamically populate a new post custom field from url?

Support General How to dynamically populate a new post custom field from url?Resolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #42437
    phillcoxonphillcoxon
    Participant

    Hi,

    This is likely a really simple solution but I haven't been able to spot it in the forum posts - I'd appreciate some advice.

    I have a "Tramp Log" CPT which records a history of past tramping trips (hiking trips).

    One of the custom fields when creating a new "Tramp Log" post is the post_title of a tramp listed in the Tramps CPT.

    I want admin users to have the ability to create a new Tramp Log post that pre-populates the name of the Tramp in the new post being created:

    https://example.com/wp-admin/post-new.php?post_type=tramp-log&tramp_name=Example Tramp Name

    Question:

    How can I retrieve the value of the tramp_name URL variable and preapply it to the tramp_name custom field before the create new post page is displayed?

    Thanks in advance!

    #42441
    PeterPeter
    Moderator

    Hello Phill,

    You can use the filter hook rwmb_{$field_id}_field_meta to pre-populate value of the field from the URL. For example:

    add_filter( 'rwmb_tramp_name_field_meta', function ( $value, $field, $saved ) {
        $value = isset( $_GET['tramp_name'] ) ? $_GET['tramp_name'] : $value;
        return $value;
    }, 10, 3);

    Read more about the filter in the documentation https://docs.metabox.io/filters/rwmb-field-meta/

    #42442
    phillcoxonphillcoxon
    Participant

    Thanks so much!

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