Hi there,
I want to access the post_id
of a post where I have an acf_form
embedded within an acf/validate_value
filter.
I’ve tried various options, but still can’t figure it out.
Could someone please point me in the right direction?
function my_validate_value( $valid, $value, $field, $input ) {
// $post_id = get_the_ID(); // outside of the loop, so won't work
// global $post;
// $post_id = $post->ID; // returns nothing
// global $wp_query;
// $post_id = $wp_query->post->ID; // returns nothing
$post_id = $_POST['acf']['id'];
if($value !== 'my-condition'){
$valid = 'Nope: ' . $post_id;
};
return $valid;
}
add_filter('acf/validate_value/key=field_5928960704f6f', 'my_validate_value', 10, 4);
Thanks in advance!
Well, I tried one more thing literally a few seconds after posting my previous thread and managed to figure it out 🙂
Hope the following helps someone; the trick seems to be $_POST['post_id']
.
function my_validate_value( $valid, $value, $field, $input ) {
$post_id = $_POST['post_id'];
if($value !== 'my-condition'){
$valid = 'Nope: ' . $post_id;
};
return $valid;
}
add_filter('acf/validate_value/key=field_5928960704f6f', 'my_validate_value', 10, 4);
Hi Dorn
Thanks for the question and follow up answer
I have a similar problem where I am trying to get the ID of the page containing the form. However, the form creates new posts so the post_id returns as 0. Has anyone found a way of retrieving.
I have tried
$post_id = $_POST['post_id'];
$url = 'https://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
$current_post_id = url_to_postid( $url );
get_queried_object_id()
global $post;
$post_id = $post->ID;
etc...
I have also tried adding additional parameters to the function but alas....
All return 0. Is what I’m attempting actually possible?
Thanks
@nickstaw you cannot get a post ID when the form is creating a new post. Unlike the WP Admin, ACF does not create a draft (autodraft) for the new post. Nothing exists until after the form is submitted.
@hube2 I don’t need to get the post ID of the new post only of the page the form is embedded in
Are you trying to get this when the form is shown or during validation?
If it is in validation (based on the title of the OP) then there will be no way to get the post ID of the page the form is embedded on unless you provide that information.
There are a couple of methods you can use to do this.
1) Probably the method I would use would be to use either the html_before_fields or html_after_fields argument of wp_form() to add html that includes a hidden field in the form and populate that hidden field with the get_queried_object_id();
2) Create a field in acf for the current post ID. Use an acf/prepare_field filter to populate the field with the current post ID and to hide the field.
Thanks @hube2
Those are excellent suggestions and is exactly what I’m looking for. Thank you. I didn’t know about 1)!
Going to be honest with you, after thinking about if for a minute you might have to go with choice 2. I’m not exactly sure that ACF will submit a hidden field that is not an ACF field. But it is still what I would try first.
The topic ‘Get Post ID of post where acf_form is embedded within acf/validate_value’ 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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.