I created a custom post type that allows my client to create a new form with specific custom fields. When the form is submitted, I use another page with a template to style the email before it is sent on. Everything is working but what I need to do is be able to set which page id is being set in the template that styles the email. I am trying to avoid creating multiple templates with each custom post page id. Is it possible to pass a variable from the custom post type page to the template page?
For example, I would have something like this:
<?php the_sub_field('choice_name', $mypostid); ?>
that would turn into something like this once the variable is passed through:
<?php the_sub_field('choice_name', 1234); ?>
I am also wanting to avoid using the Options plugin as I would have to update each time the client wanted to make a new form.
Yes, the get_field function contains a second parameter for $post_id.
Please not you cant load a sub field value, you will need to load the repeater, then loop through it’s data.
This is all explained in this article:
My issue is that I am wanting to create multiple forms but use only one template to style the form, so what I want to do is pass the post id from multiple forms to the one template.
So lets say I have 5 different forms, when the user hits submit, I need to send the post id of that specific form over the one style template page (I am trying to avoid creating multiple style templates).
I can easily pull over the post id to the style template but I am not sure how (or if it is possible) to substitute the post id:
If I have this in the form:
<input name="Name" type="text" value="<?php the_sub_field('name'); ?>" />
I then pull in this field in the style template by using:
I can do something similar with the post ID and naming it
$post_id, I’m just not sure how (or if) I can then us that in the repeater below to pull in the specific post ID
<?php if(get_field('choice_menu', '$post_id'));?> <?php while(has_sub_field('choice_menu', '$post_id'));?>
Is it possible? Does that make sense in what I am trying to do?
Ok, I got this to work. My code was actually working, I just was not able to pull in the parameter the way I originally wanted (or thought that I could). What I did was pass the Post ID parameter through the URL and then pull that parameter into my style template. Here is what I did in case it helps anyone in the future:
I passed the Post ID into the url (I.E. http://www.mysite.com/processform/?postid=1234).
I added this to my style template:
<?php $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ; $parts = parse_url($url); parse_str($parts['query'], $query); $mypostid = $query['postid']; ?>
and this is how my repeater fields looked:
<?php if(get_field('choice_menu', $mypostid)): $i = 0;?> <?php while(has_sub_field('choice_menu', $mypostid)): $i++;?> <p><?php the_sub_field('choice_name'); ?> : $check<?php echo $i; ?></p> <?php endwhile; ?> <?php endif; ?>
The topic ‘Pass the Post ID to another page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.