Support

Account

Home Forums Add-ons Options Page Combining ACF fields on save

Solved

Combining ACF fields on save

    • MikeX3

    • July 7, 2019 at 1:23 pm

    I am trying to wrap my head around how to use save_post and update_field to combine a field from a post, as well as an option field, into a new field within said post. I don’t know if I need to create the new field first and somehow hide it from the options form, or if I can have it display after save but make it non-editable.. or if acf just makes it in the background without the need for me to make it first (sorry if that doesn’t make sense).

    Here’s what I’m trying to do:

    In an acf options page, a user will enter their affiliate id: affiliate_id
    When creating a post, they will enter a product number: product_id

    When the post is saved I want a new field named “link”, within that post, to automatically combine those values so that the following is saved for the new “link” field:

    http://www.productsite.com/(product_id)/?ref=(affiliate_id)

    Here’s my guess so far as to how this might be done, but it’s really a wild guess trying to string these things together without much php knowledge:

    function my_acf_save_post( $post_id ) {
      $affid = (int) get_field('affid');
      $prod = (int) get_field('affiliate_id', 'option');
    
    $link = update_field( "http://www.productsite.com/" . $prod . "/?ref=" . $affid . ""; );
    
    add_action('acf/save_post', 'my_acf_save_post', 15);
  • If the field does not need to be editable, use standard WP function to update/get the meta value, no need for an ACF field. https://codex.wordpress.org/Function_Reference/update_post_meta https://developer.wordpress.org/reference/functions/get_post_meta/

    however, I would not do this. I would assume that there is something in the post that relates that post to the user. When outputting the post I would look up the get the ID value and append it to the url generated by get_permalink() https://developer.wordpress.org/reference/functions/get_permalink/ rather than create and/or update a field holding a link to the page.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.