Support

Account

Home Forums Front-end Issues Create Post if it doesn't exist in relationship field

Solving

Create Post if it doesn't exist in relationship field

  • I would like the option to create a post if it doesn’t exist when using a relationship field. Many times For my application I need to relate two item types that are to be created at the same time so I need to create one while creating the other from the same page.

    I thought about having two forms on a front end page that submit via Ajax and when one is created it then makes the relationship in the other. I only need this one-way relationship, it doesn’t need to be bi-directional.

  • Hi @maxaud

    I’m afraid ACF doesn’t have the ability to do that. What you can do is creating a dummy form to add a custom post when editing the other. You can use the acf/save_post hook to get the posted data, create it and then add the relation to the other post type using the update_field() function.

    I hope this makes sense 🙂

  • Since the relationship field does an admin Ajax call to query for a post to relate to, is there a way programmatically add to the returned results or is there a way for me to create a new post and then set the relationship field to the new result?

    Example:
    Two forms on one page. Form A has the relationship field and makes post type 1. Form B has a form that creates a new post type 2 via Ajax so the page doesn’t reload. When a new post type 2 is created by form 2 I want to then set the relationship field in form A to this newly created post type 2 without user action.

    I would need to simulate searching and setting the relationship after its created without the user actually doing so.

  • Hi @maxaud

    If you want to do it via Ajax, you need to check how relationship field works, and it’s not an easy task to do as relationship field has a complex system.

    What I suggest you is to use a repeater to add a dummy post form to create the new post type 2. When the users save the post type 1, you need to use the acf/save_post hook to loop through the posted data and create them. After that, you can update the relationship field on the post type 1 with the newly created post type 2.

    You can also clear the posted data after that so the repeater form won’t looks ugly.

    I hope this makes sense 🙂

  • It makes sense but not exactly a smooth user journey.

    Its my clients who will have to do this and they aren’t so hot on WordPress / data input to begin with. I just think a message saying ‘no matches — CREATE NEW POST’ would make data inputting and relating content pretty foolproof and very quick.

    I love ACF and have a pro license. I just find its hits a wall sometimes and then we have to turn to another less reliable plugin or build it ourselves.

    I rely (and pay for) on great plugins like ACF to make these features easy to add. Even being able to add a submit button via the interface would be amazing. And backwards relating from a post that has been related to another via post object or post id…

  • Hi @v3nt

    As you already guessed, I’m afraid that feature does not exist yet. If you need this feature now, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/. Otherwise, you can always submit a feature request by opening a new ticket here: https://support.advancedcustomfields.com/new-ticket. That way, your request can be passed directly to the plugin author.

    Thanks 🙂

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

You must be logged in to reply to this topic.