Dynamically populate date of a post [RESOLVED]

Hi,
I created a report form for my posts so members can be able to report a post submitted by another member if it violates against the website rules. I need to include the date of the post in my report form.

Anyone here can help me how to dynamically populate the date of a post? Thank you.

Hello Jim. With a little more information, we should be able to come up with something. Are you embedding the same form on all posts, so that people can report the Post from the post itself, using the form?

If that is true, you want to store the date that that specific WordPress post’s date published in the entry created by submitting the form embedded in that post?

Hi Chris,

Yes that is correct.

Yes sir, that’s what I want to achieve.

Thank you. In that case, you can use some code to retrieve the date from the post and populate a field in the form. Add a single line text field to the form, and on the Advanced tab select the option to “Allow field to be populated dynamically”. Then give it this parameter name:

post_date

Then, you can use this code to populate that field from the post date of the post where you are showing the form:

add_filter( 'gform_field_value_post_date', 'populate_post_date' );
function populate_post_date( $value ) {
    global $post;
    // change 'F j, Y' to whatever format you like, based on this 
    // https://www.php.net/manual/en/function.date.php
    return get_the_date( 'F j, Y' );
}

That populated a form field with the date in the format November 18, 2016 in the form I had embedded on an old post. If you need anything else, please let us know.

1 Like

It worked! Thank you so much! :smile:

1 Like