Support

Account

Home Forums General Issues Looking to display 5 ACF fields' values in one field, while adding a post.

Solving

Looking to display 5 ACF fields' values in one field, while adding a post.

  • Hello,

    I have a post when of a certain category, has some fields on the backend: a select, three text fields, and a radio. Super simple. What I want to do, is, while a post is being authored, and those fields’ values added (all these fields are required), that a new field – maybe a text area or WYSWYG – or code – automatically gets each of those values. Ideally, as they are being typed in, but if not then, then after the post is published, they would be shown on the post after it refreshes from being published.

    The point of this is to as quickly as possible, with a few clicks as possible, to
    – make the post
    – copy those values all at once
    – go to our email marketing dashboard (not WordPress)
    – paste those values into a new email.

    Example of these fields and a value for each on the add post page:
    Radio: BUY
    Select: EURAUD
    Open Price: 1.5
    Take Profit Price: 1.6
    Stop Loss Price: 1.8

    Example of my goal, where those values are each displayed together, in whatever form is possible, so they can be selected and copied at once:
    Textarea:
    BUY EURAUD 1.5 1.6 1.8

    Once that is working then i would add labels, maybe some HTML / inline CSS.

    Is this doable, and any ideas how? Again, ideally they update as they’re being typed, but really, it would also be fine if they’re shown on page refresh after publish refresh.

    Thanks!

  • I would probably use a message field because your goal is for copying and pasting.

    The easiest way to do this is to create an acf/prepeare_field filter for the message field.

    The message field does not need to have any value to begin with. In you filter you would get the values of the other fields and then and the values format $field[‘message’] as you want.

    This will only work after a post is saved. I is possible to do this as values are added or selected, but I can’t give you any examples of doing this. You’d need to add custom JavaScript and do quite a bit of JS coding to get the values of the fields as they are updated and then update the message field content.

  • Hi John,

    Thanks for the reply. I am working on it, and can you take a look? Something is missing i think.

    Also is $field[‘message’] what i use to get the field to show on the page?

    <?php
    function my_acf_prepare_field( $field ) {
    
        // Lock-in the value "Example".
        // if( $field['value'] === 'Example' ) {
        //     $field['readonly'] = true;
        // };
        // return $field;
        $buy_or_sell = get_field('buy_or_sell');
        $pair = get_field('pair');
    }
    
    // Apply to fields named "example_field".
    add_filter('acf/prepare_field/name=buy_or_sell', 'my_acf_prepare_field');
    add_filter('acf/prepare_field/name=pair', 'my_acf_prepare_field');
  • Something like this

    
    add_filter('acf/prepair_field/name=MESSAGE_FILED_NAME', 'change_message_MESSAGE_FILED_NAME')
    function change_message_MESSAGE_FILED_NAME($field) {
      $some_value = get_field('some_field');
      $some_other_value = get_field('some_other_field');
      $field['message'] = $some_value.' '.$some_other_value);
      return $field;
    }
    
  • Thanks again John. I’m almost there..

    This is my code made from your last post:

    // get some values from Trade Alert Post, and put them in a message field for Jen to copy and paste into email / Jilt (/ eventually MailChimp maybe)÷
    add_filter('acf/prepair_field/name=MESSAGE_FIELD_NAME', 'change_message_MESSAGE_FIELD_NAME');
    function change_message_MESSAGE_FIELD_NAME($field) {
      $pair = get_field('pair');
      $buy_or_sell = get_field('buy_or_sell');
      $field['message'] = ($pair.' '.$buy_or_sell);
      return $field;
    }

    And i made another field a WYSWYG with a default value of [acf field="MESSAGE_FIELD_NAME"] thinking after post saves and refreshes, it would get the value, but no go. How would i display the $field from above?

  • The code I supplied with work for a message field type. You also need to change the filter and function name to replace all instances of “MESSAGE_FIELD_NAME” with the real name of the field.

    • benblue

    • June 15, 2021 at 11:29 pm

    Hi John,

    I actually named my field just as you typed for simplicity’s sake, then will name it logically when working.

    I put your code in ‘functions.pho’ which I realize should be in the post instead. I’ll try again; thanks!

  • @hube2 I am still not clear – do i put the code in the template php file for the posts that have the fields whose values i am putting into the message field? And are we talking about the message field displaying its values on the front end or the back end? Backend is the goal.

    I added a message field – and it doesn’t have a name field, only label, so i named the label MESSAGE_FIELD_NAME and this field is on the same posts as the fields it’s supposed to be showing. When the post is saved and the page refreshes, that’s when i’m hoping to see the message field’s value, but there’s nothing.

    For some reason this hasn’t clicked for me yet. Can you help me more?

  • the code should go in functions.php.

    Altering $field[‘message’] will alter the message displayed in the admin.

    I forgot about message fields not having names, in this case you will need to use the field key variant of the filter, see the documentation.

  • I got it all sorted and working! Thank you. I was thinking the field would be similar to a text area, where the text could be changed.

    How easy is it to have what is going to the new filed to display in a WYSWYG field? Maybe make a WYSWYG and set its default value to a shortcode (i’ll create it) that returns the value the new field is showing?

    the new field with concatenated fields

  • You can do the same thing with any type of field. For a text area instead of setting $field[‘message’] you would set $field[‘value’]. I was under the assumption that you just wanted to be able to copy and paste the value. If you want to use the value is some other way then you would need a different type of field. But this is really not needed, if you want to use the values of the other fields outside of the admin you just need to get the values and combine them.

    • benblue

    • June 17, 2021 at 10:16 am

    Got it – totally makes sense now. If i did want to do the exact same thing but with textarea, for example, would my filter still be acf/prepare_field/ or what filter would i use?

  • I would still use the prepare_field filter. There are a lot of filters in ACF and there are several others that could be used and would have different effects, like acf/load_value, acf/load_field

Viewing 13 posts - 1 through 13 (of 13 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.