PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Taking data from date field and using it elsewhere in Wordpress

  1. Jade
    Member

    Hi,

    I am working on a Wordress-based website where guests contribute blog posts about events. Each blog post is about an event, and each event has an Event Date. I use a gravity form for people to input information like Event Date and turn it into a post, and this works great. However, I'm trying to use the Event Date value that people enter into the form elsewhere in the website...specifically to selectively display posts about events that have already passed, or display only posts about events happening today.

    How can I reference the value of the Event Date field? Is there a key that refers to the value entered into this field? Thank you!

    Posted 13 years ago on Monday January 10, 2011 | Permalink
  2. If you are using Gravity Forms to create WordPress posts you should be using Post Fields.

    If you are using the Date Field and not the Post Custom Field configured to the Date Field Type, then you need to reconfigure how your form is setup.

    You should use the Post Custom Field, edit the field and select Date from the Field Type drop down. Then give the custom field a name (or select from an existing name if you already have a date custom field).

    Date for this field will then be stored as a custom field for the post using the name you set as the key. After that you need to look up how to use custom fields and learn how use them in your theme.

    See this documentation:

    http://codex.wordpress.org/Custom_Fields

    You may also want to look at getting a good book on WordPress development that would discuss post meta and post custom field usage in themes.

    Posted 13 years ago on Monday January 10, 2011 | Permalink
  3. jbdurand
    Member

    I have a similar problem. I want the user to set the date of his post within the form so that it will be displayed in a simile timeline.
    I tried the "advanced field date" and the "custom post field" as you explained but the post is still sent at today's date.
    is there a trick to do that in the "Create Post content template" / "insert form field"

    I am new to gravity, I love it and i miss this point.

    I hope you will help me to get out of this problem!
    best regards from France.

    Posted 13 years ago on Friday January 28, 2011 | Permalink
  4. Which date are you trying to change? The date that the post is created?

    Posted 13 years ago on Saturday January 29, 2011 | Permalink
  5. jbdurand
    Member

    Hello Alex,
    Yes I wish to change the date of post creation (timestamp)
    (for example if the event was "Macintosh launch" the date of the post should be in 1984 so that it would display in the timeline in 1984.)
    is it possible?

    Posted 13 years ago on Saturday January 29, 2011 | Permalink
  6. I think the following should do the trick for you. Place it in your theme's function.php and don't forget to replace the IDs (form ID and field ID) with your actual IDs.

    add_filter("gform_post_data", "change_post_date", 10, 3);
    function change_post_date($post_data, $form, $entry){
    
        //Only change post date on form id 124.
        //NOTE: Replace 124 with your actual form id
        if($form["id"] != 124)
           return $post_data;
    
        //NOTE: Replace 3 with your date field id. You can find your field id by inspecting the field's input tag
        $date = $entry[3];
    
        $post_data["post_date"] = date("Y-m-d", strtotime($date));
        return $post_data;
    }
    Posted 13 years ago on Saturday January 29, 2011 | Permalink
  7. jbdurand
    Member

    Alex,
    thank you very much for the code.
    I tried it but it seems not to work for the moment. maybe I did something wrong.

    I copied the code in the functions.php then I put the form number 3 and the field number 26
    and all the new post sent are now at january 1rst 1970...
    could it be a date format problem? (I have a MM DD YY format)

    "<label class='gfield_label' for='input_3_26'>Date</label><div class='clear-multi'><div class='gfield_date_month ginput_container' id='input_3_26'><input type='text' maxlength='2' name='input_26[]' id='input_3_26.1' value='' tabindex='2' /><label for='input_3_26.1'>MM</label></div><div class='gfield_date_day ginput_container' id='input_3_26'><input type='text' maxlength='2' name='input_26[]' id='input_3_26.2' value='' tabindex='3' /><label for='input_3_26.2'>DD</label></div><div class='gfield_date_year ginput_container' id='input_3_26'><input type='text' maxlength='4' name='input_26[]' id='input_3_26.3' value='' tabindex='4' /><label for='input_3_26.3'>YYYY</label></div"

    Posted 13 years ago on Saturday January 29, 2011 | Permalink
  8. jbdurand
    Member

    I enclose the code that I inserted. maybe I did incorrect modifications:

    add_filter("gform_post_data", "change_post_date", 10, 3);
    function change_post_date($post_data, $form, $entry){

    //Only change post date on form id 124.
    //NOTE: Replace 124 with your actual form id
    if($form["id"] != 3)
    return $post_data;

    //NOTE: Replace 3 with your date field id. You can find your field id by inspecting the field's input tag
    $date = $entry[27];

    $post_data["post_date"] = date("Y-m-d", strtotime($date));
    return $post_data;
    }

    ps: the date code is now 27 because I tried to create a custom post date field. It didn't work better than the date field

    Posted 13 years ago on Sunday January 30, 2011 | Permalink
  9. Yes, there is a problem with the date formatting. I was assuming you were using the single input date field with the calendar.

    Replace the following lines.

    $date = $entry[27];
    
    $post_data["post_date"] = date("Y-m-d", strtotime($date));

    with this...

    $post_data["post_date"] = $entry["26.3"] . "-" . $entry["26.1"] . "-" . $entry["26.2"];
    Posted 13 years ago on Sunday January 30, 2011 | Permalink
  10. jbdurand
    Member

    Alex I tried the modification but didn't work. there still might be a date format problem.

    currently in my form the form number is 3, the date number is 28, the input date type is "date field" the date format is "dd/mm/yyyy"

    so I inserted in the following text in function.php :

    add_filter("gform_post_data", "change_post_date", 10, 3);
    function change_post_date($post_data, $form, $entry){

    //Only change post date on form id 124.
    //NOTE: Replace 124 with your actual form id
    if($form["id"] != 3)
    return $post_data;

    //NOTE: Replace 3 with your date field id. You can find your field id by inspecting the field's input tag
    $post_data["post_date"] = $entry["28.3"] . "-" . $entry["28.2"] . "-" . $entry["28.1"];
    return $post_data;
    }

    the result is that the posts are sent a today date. not working.

    here is the code of the form for the date input:

    <label class='gfield_label' for='input_3_28'>Date</label><div class='clear-multi'><div class='gfield_date_day ginput_container' id='input_3_28'><input type='text' maxlength='2' name='input_28[]' id='input_3_28.2' value='' tabindex='2' /><label for='input_3_28.2'>DD</label></div><div class='gfield_date_month ginput_container' id='input_3_28'><input type='text' maxlength='2' name='input_28[]' id='input_3_28.1' value='' tabindex='3' /><label for='input_3_28.1'>MM</label></div><div class='gfield_date_year ginput_container' id='input_3_28'><input type='text' maxlength='4' name='input_28[]' id='input_3_28.3' value='' tabindex='4' /><label for='input_3_28.3'>YYYY</label></div>

    the other thing is that I will have several gravity forms which should send post with a user modified date.
    so What would be the code for several forms and Date Input Type, date picker, calendar, dd/mm/yyyy?

    Posted 13 years ago on Sunday January 30, 2011 | Permalink
  11. Lets get this first one working first. Can you send me an export of your form to alex@rocketgenius.com

    Posted 13 years ago on Monday January 31, 2011 | Permalink
  12. jbdurand
    Member

    hello Alex
    I sent you the exported form by mail

    Posted 13 years ago on Monday January 31, 2011 | Permalink
  13. The first code snippet I sent is the right one.
    It worked for me here. My post ended up with the right date.
    I am not sure why it wouldn't work for you. What you can try to do is switch the date format and see if that makes a difference.

    replace

    date("Y-m-d", strtotime($date));

    with ....

    date("Y-d-m", strtotime($date));

    If that still doesn't work, I am out of options.

    Following is the code that worked for me

    add_filter("gform_post_data", "change_post_date", 10, 3);
    function change_post_date($post_data, $form, $entry){
    
        //Only change post date on form id 124.
        //NOTE: Replace 124 with your actual form id
        if($form["id"] != 126)
           return $post_data;
    
        //NOTE: Replace 3 with your date field id. You can find your field id by inspecting the field's input tag
        $date = $entry[28];
    
        $post_data["post_date"] = date("Y-m-d", strtotime($date));
        return $post_data;
    }
    Posted 13 years ago on Monday January 31, 2011 | Permalink
  14. jbdurand
    Member

    still not working...
    I tried to import the form in another blog with another theme (twentyten) and it still didn't work. the date is 1970.

    I modified the "functions.php" of the theme file and you wrote "function.php".
    I am wrong here?
    I pasted your code at the end of this functions.php text.

    it's very frustrating...

    I can give you by mail the admin code eventually. something is wrong somewhere...

    Posted 13 years ago on Monday January 31, 2011 | Permalink
  15. functions.php is correct.
    I will need to take a deeper look at this one for you. I will need an admin WP and FTP login to be able to see what is going on. Send them to alex@rocketgenius.com.

    Posted 13 years ago on Tuesday February 1, 2011 | Permalink
  16. jbdurand
    Member

    Alex, I have Discovered a critical problem : The date of a post (timestamp)
    cannot be set before 1970 in wordpress.
    By consequence using the timestamp date field to enter historical events in the simile timeline is not The right solution there.
    In the simile Timeline wordpress plugin, a widget in the posting page enable to enter dates (start, end...).
    But these fields are not custom type fields, they are recording data into custom database tables.
    Do you think it could be possible to write data directly into database fields from gravity?

    info on the wp simile timeline plugin here:
    Simile timeline plugin : http://wordpress.org/extend/plugins/wp-simile-timeline/
    http://groups.google.com/group/wp-simile-timeline

    Posted 13 years ago on Wednesday February 2, 2011 | Permalink
  17. It would be possible using the gform_post_submission hook, but you will need some PHP experience to pull it off. We can recommend a developer if this is something you are willing to pay for.

    Posted 13 years ago on Wednesday February 2, 2011 | Permalink