Populate fields with custom post data [RESOLVED]

So here is my problem. I need to automatically populate a form with the data from a post. I get the basic of how to make a drop down with all posts in a custom post types. But no I need to populate other fields with data from the selected post. Is there some documentation or a way to easily do that? I have been googling but can’t find anything.

One of our community members just shared this:

https://typewheel.xyz/capture-post-content-gravity-form-field/

That is not what I mean.

You select a post from a dropdown that is dynamically generated. When that post is selected, the rest of the form should be filled with data from that post.

The method I outline in that post could work as half of your solution. However, instead of matching content from the existing post, you’d need to use the WP REST API to fetch the content from the selected post to use that, instead.

2 Likes

Thank you, while your article didn’t help me that much, recommending the REST API put me on the right track. What I ended up doing is after populating the select I use jquery to watch that select for change and when it changes I use an ajax call to run a database query in php, the results of which I insert into the input:

jQuery(document).ready(function( $ ) {

$("#input_9_15").change(function() {
	var bcVar = $('#input_9_15').val();

	$.ajax({
		type: "POST",
		url: "/wp-content/themes/twentysixteen-child/barcode.php",	
		data:{"barvar":bcVar},
		dataType: 'json',
		success: function(result){
			$("#input_9_16").val(result["description"]);
		}

	});
});

});

This is the content of barcode.php (the file called in the ajax):

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
$barvar = $_POST['barvar'];

$barcode_data = get_page_by_post_name($barvar, object, 'barcodes');
$barcode_id = $barcode_data->ID;

$throwback = array(
    'description' => get_the_excerpt($barcode_id),
 );
echo json_encode($throwback);
2 Likes

Hi Hellenique, you’re looking for Gravity Forms Populate Anything. It’ll make this whole process even easier. You can…

  1. Dynamically populate a list of posts into a Drop Down.
  2. Populate post properties and post meta into any field on your form based on the selected post in the Drop Down.

If you have any follow-up questions, drop us line.

2 Likes

That is something to look into in the future, but for now the current self-built solution does the job.