Home Forums Front-end Issues Pre-fill acf_form fields based on query parameter


Pre-fill acf_form fields based on query parameter

  • I’m working on a setup with acf_form on my front end where the following workflow occurs.

    If a query such as “” is equal to 1, then the page loads that query value using WordPress get_query_var, and adds the “isset” class to the body element on the page.

    If the “isset” class is present on the page, a Javascript event fires which clicks a checkbox for a True / False field titled “Add Relational Post?” in the ACF Form; this checkbox is hidden from the viewer via CSS, and has a conditional field attached to it. I don’t want the user checking it, just want to set if this query var is present.

    If the “Add Relational Post?” box is checked, then it fires a Conditional Logic parameter and a second Relationship field appears. If the box is not checked, the relationship field remains hidden.

    The Relationship field in this case only allows for 1 and only 1 post to be selected, sort of a “parent” of the new post being created.

    What I’d like to do, is based off of a second, separate query var, such as “”, is to find Post ID 20 in the Relationship field, and pre-select that field when the acf_form renders. It looks like each of the items in the Relationship field have a “data-id=POST_ID” element associated with them, so it looks like a good place to start.

    Actually, in writing this post, I may have figured something out that will work; I’ll report back if it does, but I welcome any suggestions!

  • Hi @pkarjala

    Interesting workflow 🙂

    I got some tips for you that you may or may not know about..

    A relationship field uses ajax to find posts so you might not be able to just find the post you’re looking for in the already loaded posts if there’s many.

    There’s a js event you can use to start up your own js code when the relationship field is shown:

    acf.add_action('show_field', function( $field, context ){
    	// context is a string of either 'tab' or 'conditional_logic'
    	// do something to $field

    I’m thinking you’d have to run some custom AJAX on this event. Look up the GET parameter (can be done via JS if you like) and run AJAX to find all the information needed to inject the post information into the relationship field.

  • I actually ended up going in a different direction. Since I only needed one post to be the Related post, I changed the field type from Relationship to Post Object. From here, when the page loads with the aforementioned query tags, I use javascript to set the value of #acf-field_<number>-input field to the post_id of the parent. Then, when saving, it properly sets the parent post.

    This is a bit hackish, but does what I need it to.

  • Cool! Yeah the post object is much easier to manipulate if you only require one..
    Since ACF started to use select2 for the dropdowns it’s just as easy to use as the relationship field.. (earlier on I used to utilize relationship field even if I only needed 1 due to the interface).

    Best of luck in your project!

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

The topic ‘Pre-fill acf_form fields based on query parameter’ is closed to new replies.