Populate field with ACF value
-
Hello,
I have a custom post type (landing) with an ACF field “post object” (solution_rel) with the possibility to add multiple posts of another CPT (solution).
I would like to populate my field “text-2” of the form is displayed in the post “landing” with the titles of the posts referenced by the ACF field.
I have tried many solutions without success.
Can you help me to do this?
Thanks in advance for your help!
Have a good day.
-
Hi @besky,
I hope you are keeping well and thank you for reaching out to us.
Could you please provide an export of the form along with the field groups and post types to give a closer look at the scenario?
I would like to populate my field “text-2” of the form is displayed in the post “landing” with the titles of the posts referenced by the ACF field.
Could you please provide further clarification on this requirement, perhaps with an example? Are you intending to embed the form within the “landing” post type, and do you require the “text-2” field of the form to display the titles of the posts?
We look forward to hearing back from you.
Kind Regards,
Nebu JohnHi @wpmudevsupport14 !
Sorry for my late reply.
Here is the form export :
{ "type": "form", "data": { "fields": [ { "id": "text-1", "element_id": "text-1", "form_id": "wrapper-7806-5995", "parent_group": "", "type": "text", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-7806-5995", "input_type": "line", "limit_type": "characters", "field_label": "Société", "placeholder": "", "limit": "", "required": "1" }, { "id": "name-1", "element_id": "name-1", "form_id": "wrapper-3721-3461", "parent_group": "", "type": "name", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-3721-3461", "field_label": "Nom", "placeholder": "", "prefix_label": "Préfixe", "fname_label": "Prénom", "fname_placeholder": "E.g. John", "mname_label": "Deuxième prénom", "mname_placeholder": "E.g. Smith", "lname_label": "Nom de famille", "lname_placeholder": "E.g. Doe", "prefix": "", "fname": "", "mname": "", "lname": "true", "required_message": "Le nom est obligatoire", "prefix_required_message": "Le préfixe est obligatoire.", "fname_required_message": "Le prénom est obligatoire.", "mname_required_message": "Le deuxième prénom est obligatoire.", "lname_required_message": "Le nom est obligatoire.", "multiple_name": "true", "fname_required": "1", "lname_required": "1" }, { "id": "email-1", "element_id": "email-1", "form_id": "wrapper-3663-5173", "parent_group": "", "type": "email", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-3663-5173", "validation": "1", "placeholder": "", "field_label": "Adresse de messagerie", "required": "1", "validation_message": "Merci de vérifier votre saisie" }, { "id": "phone-1", "element_id": "phone-1", "form_id": "wrapper-9399-1949", "parent_group": "", "type": "phone", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-9399-1949", "required": "1", "limit": "10", "limit_type": "characters", "validation": "none", "field_label": "Téléphone", "placeholder": "", "phone_national_country": "FR" }, { "id": "gdprcheckbox-1", "element_id": "gdprcheckbox-1", "form_id": "wrapper-7867-9110", "parent_group": "", "type": "gdprcheckbox", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-7867-9110", "required": "true", "field_label": "", "gdpr_description": "J’accepte la politique de confidentialité", "required_message": "Ce champ est obligatoire, veuillez cocher la case." }, { "id": "hidden-1", "element_id": "hidden-1", "form_id": "wrapper-2579-7586", "parent_group": "", "type": "hidden", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-2579-7586", "field_label": "page", "default_value": "embed_title" }, { "id": "text-2", "element_id": "text-2", "form_id": "wrapper-5414-1731", "parent_group": "", "type": "text", "options": [], "cols": "12", "conditions": [], "wrapper_id": "wrapper-5414-1731", "input_type": "line", "limit_type": "characters", "field_label": "Solution", "placeholder": "", "description": "%solution%" } ], "settings": { "pagination-header": "nav", "paginationData": { "pagination-header-design": "show", "pagination-header": "nav" }, "formName": "Demande de devis", "version": "1.29.3", "form-border-style": "none", "form-padding": "", "form-border": "", "fields-style": "open", "validation": "on_submit", "akismet-protection": "0", "form-style": "material", "enable-ajax": "true", "autoclose": "true", "submission-indicator": "show", "indicator-label": "Soumission en cours…", "form-type": "default", "submission-behaviour": "behaviour-redirect", "thankyou-message": "Merci de nous avoir contacté, nous reviendrons vers vous sous peu.", "submitData": { "custom-submit-text": "Envoyez votre demande", "custom-invalid-form-message": "Erreur?: votre formulaire n’est pas valide, veuillez corriger les erreurs?!" }, "validation-inline": "1", "form-expire": "no_expire", "form-padding-top": "0", "form-padding-right": "0", "form-padding-bottom": "0", "form-padding-left": "0", "form-border-width": "0", "form-border-radius": "0", "cform-label-font-family": "Roboto", "cform-label-custom-family": "", "cform-label-font-size": "12", "cform-label-font-weight": "bold", "cform-title-font-family": "Roboto", "cform-title-custom-family": "", "cform-title-font-size": "45", "cform-title-font-weight": "normal", "cform-title-text-align": "left", "cform-subtitle-font-family": "Roboto", "cform-subtitle-custom-font": "", "cform-subtitle-font-size": "18", "cform-subtitle-font-weight": "normal", "cform-subtitle-text-align": "left", "cform-input-font-family": "Roboto", "cform-input-custom-font": "", "cform-input-font-size": "16", "cform-input-font-weight": "normal", "cform-radio-font-family": "Roboto", "cform-radio-custom-font": "", "cform-radio-font-size": "14", "cform-radio-font-weight": "normal", "cform-select-font-family": "Roboto", "cform-select-custom-family": "", "cform-select-font-size": "16", "cform-select-font-weight": "normal", "cform-multiselect-font-family": "Roboto", "cform-multiselect-custom-font": "", "cform-multiselect-font-size": "16", "cform-multiselect-font-weight": "normal", "cform-dropdown-font-family": "Roboto", "cform-dropdown-custom-font": "", "cform-dropdown-font-size": "16", "cform-dropdown-font-weight": "normal", "cform-calendar-font-family": "Roboto", "cform-calendar-custom-font": "", "cform-calendar-font-size": "13", "cform-calendar-font-weight": "normal", "cform-button-font-family": "Roboto", "cform-button-custom-font": "", "cform-button-font-size": "14", "cform-button-font-weight": "500", "cform-timeline-font-family": "Roboto", "cform-timeline-custom-font": "", "cform-timeline-font-size": "12", "cform-timeline-font-weight": "normal", "cform-pagination-font-family": "", "cform-pagination-custom-font": "", "cform-pagination-font-size": "16", "cform-pagination-font-weight": "normal", "payment_require_ssl": "", "submission-file": "delete", "form_name": "demande-de-devis", "form_status": "publish", "redirect-url": "{hidden-1}", "newtab": "newtab_thankyou", "honeypot": "1", "use-custom-css": "1", "custom_css": ".forminator-row-last {\n text-align: right;\n}\n\n.forminator-button-submit {\n border-radius: 50px;\n padding: 1rem 1.5rem;\n font-size: 1.1rem;\n font-weight: 500;\n}\n\n .forminator-input {\n border: 1px solid #fff;\n padding-left: .5rem;\n }\n \n.forminator-label:not(.forminator-floating--textarea) {\n margin-left: .5rem;\n}\n\n#textarea-1 {\n margin-top: 1.5rem;\n}\n\n.forminator-floating--textarea {\n padding-top: 10px;\n}\n\n/* Chrome, Safari, Edge, Opera */\ninput::-webkit-outer-spin-button,\ninput::-webkit-inner-spin-button {\n -webkit-appearance: none;\n margin: 0;\n}\n\n/* Firefox */\ninput[type=number] {\n -moz-appearance: textfield;\n}", "cform-color-settings": "true", "cform-label-color": "#ffffff", "label-helper-color": "#ffffff", "input-border": "#ffffff", "input-placeholder": "#ffffff", "input-color": "#ffffff", "button-submit-background-static": "#ff7471", "input-border-hover": "#e6e6e6", "input-border-active": "#e6e6e6", "button-submit-background-hover": "#ed6868", "button-submit-background-active": "#ed6868", "radio-label": "#ffffff", "radio-border-static": "#ffffff", "store_submissions": "1", "sc_email_link": "1", "sc_message": "<p>Your form has been saved as draft and a resume link has been generated so you can return to the form anytime within {retention_period} days from today. Copy and save the link or enter your email address below to have the link sent to your mail.</p><p>These fields weren't saved to your submission draft: Paypal, Stripe, Signature, Password, Captcha, and Upload. Kindly fill them out before submitting the form.</p>", "notification_count": 3, "previous_status": "publish" }, "client_id": null, "integration_conditions": [], "behaviors": [ { "slug": "behavior-6367", "autoclose": "1", "autoclose-time": "5", "newtab": "sametab", "submission-behaviour": "behaviour-thankyou", "thankyou-message": "<p>Merci de votre demande !</p>\n<p>Nos experts reviendront vers vous très vite.</p>", "redirect-url": "" } ], "notifications": [ { "slug": "notification-1234-4567", "label": "Admin Email", "email-recipients": "default", "recipients": "[email protected]", "email-subject": "Nouvelle entrée de formulaire pour {hidden-1} ({text-2})", "email-editor": "<p>Vous avez une nouvelle soumission de formulaire : <br>{all_fields} <br>---<br>Ce message a été envoyé depuis {site_url} sur la page {hidden-1}.</p>", "email-attachment": "true", "conditions": [ { "element_id": "text-1", "rule": "starts", "value": "Bessonweb" } ], "cc-email": "[email protected],[email protected]", "from-name": "", "form-email": "", "replyto-email": "", "bcc-email": "", "condition_action": "dont-send", "condition_rule": "all" }, { "slug": "notification-1467-2289", "label": "Email prospect", "email-subject": "Votre demande sur company", "email-editor": "<p>Bonjour {name-1-first-name},</p>\n<p>Nous avons bien réceptionné votre demande d'informations à propos des solutions company.</p>\n<p>Nous reviendrons vers vous dans les plus brefs délais.</p>\n<p>L'équipe company.</p>\n<p><a href=\"tel:+33143343434\">0143343434</a></p>", "recipients": "{email-1}", "from-name": "company", "form-email": "", "replyto-email": "[email protected]", "conditions": [], "cc-email": "", "bcc-email": "" } ] }, "status": "publish", "version": "1.29.3" }
You can see, the field “text-2” is simply used for display the name of the solution that the lead want a quotation or a demo.
The solutions are linked to the landing page with a ACF field that use the content type “solutions” for choose the wanted solution by the editor of the page.
So, in the ACF field we have per example :
– Solution 1
– Solution 2
– Solution 3These are “solutions” content types.
So I resume :
- The editor create a landing page and choose the solution(s) linked in the ACF field
- The form is displayed in the landing page in front but the field “solution” (text-2) is not visible for users.
- I want to populate the field “text-2” with the title(s) of the solution(s) to display it in the admin email.
I hope this is more clear for you.
Thanks for your help!
Hi @besky
Thanks for response!
If I correctly understand the goal, this additional code should do the trick:
<?php add_filter( 'forminator_field_text_markup', 'forminator_current_postobj_to_text', 11, 2 ); function forminator_current_postobj_to_text( $html, $field) { // CONFIG $field_name = 'solution_rel'; // field name of your post object ACF field $text_id = 'text-2'; // ID of the form text field to put titles into if ( $field['element_id'] !== $text_id ) { return $html; } $the_posts = get_field( $field_name, get_the_ID() ); $titles = ''; foreach ( $the_posts as $key => $the_post ) { $titles .= $the_post->post_title . ', '; } $html = str_replace( 'value=""', 'value="'.$titles.'"', $html ); return $html; }
To add it to the site;
– create an empty file with a .php extension (e.g. “forminator-acf-postobj-titles.php” or similar) in the “/wp-content/mu-plugins” folder of your site’s WordPress install
– cope above code and past it into the file
– you may need to do some small adjustments in the code
a) in this line
$field_name = 'solution_rel'; // field name of your post object ACF field
you set the name of your “post object” ACF field to fetch data from; I assume it’s “solution_rel”
b) in this line
$text_id = 'text-2'; // ID of the form text field to put titles into
you set an ID of the form text field to put data into; according to your request I assumed it’s text-2
c) and in this line
$titles .= $the_post->post_title . ', ';
you may want to adjust the “separator”; I set it to comma so titles of posts – if there are multiple – would be separated by comma; you can replace that character with something else.
– finally, just save the file and clear all cache on site/server.
WHat would happen now is that when a given page is visited and the form is there, this code is executed and checks current post/page ID, then reads the value of that ACF post object field for this current post, takes post titles from it and puts them in the text-2 field.
Note: this will work if the post object field in ACF has the “Return Format” set to “Post Object”. If it’s set to “Post ID” then the code would require changes.
Best regards,
AdamHi @besky,
Since we haven’t heard from you for a while. I’ll mark this thread as resolved for now. Please feel free to open a new thread if you have new queries.
Kind Regards
NithinHello,
Sorry for the late back.
I confirm it’s work good!
Thanks you a lot for your help.
- The topic ‘Populate field with ACF value’ is closed to new replies.