Skip Navigation

[Resolved] Populate select options dynamic (Gravity Forms). How to format date?

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by ivoR 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#504200
explaination.png

I am trying to fill the select options in my gravity form with date (repeatable custom field) from the current page. This is what I got so far in functions.php


add_filter('gform_pre_render_1', 'populate_posts');
function populate_posts($form){

    foreach($form['fields'] as &$field){

        if($field['type'] != 'select' || strpos($field['cssClass'], 'gravity-datum-select') === false)
            continue;

        $posts = get_post_custom_values( 'wpcf-datum' );

        $choices = array(array('text' => 'Selecteer een datum', 'value' => ' '));

        foreach($posts as $post){
            $date = $post->wpcf-datum;
            $choices[] = array('text' => $date, 'value' => $date);
        }

        $field['choices'] = $choices;

    }

    return $form;
}

It seems to work. I get 2 values in my dropdown. But the value is '0' instead of the date. What am I missing here?

#504268

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - Types stores date field value as a Unix timestamp. So you may need to convert your field value to date using PHP date function.

If you can share problem URL and access details it will help me to know what data stored in your field and how you should display it.

1)
Could you please share problem URL

2)
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#504298

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use strtotime() function.

 $date = date('d-m-Y', strtotime($post->wpcf-datum-workshop));

More info:
hidden link

#504306

Good suggestion, no luck sadly. Still showing 01-01-1970

#504309

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I need test site with problem URL and access details to check your code. There is no other way to debug further.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#504396

I noticed a typo in my latest code. Here is an updated one:

add_filter('gform_pre_render_1', 'populate_posts');
function populate_posts($form){

    foreach($form['fields'] as &$field){

        if($field['type'] != 'select' || strpos($field['cssClass'], 'gravity-datum-select') === false)
            continue;

        // $posts = get_post_custom_values( 'wpcf-datum-workshop' );

        $choices = array(array('text' => 'Selecteer een datum', 'value' => ' '));

        $child_args = array(
        'post_type' => 'workshop-datum',
        'numberposts' => -1,
        'meta_key' => 'wpcf-datum-workshop',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(array('key' => '_wpcf_belongs_workshop_id', 'value' => get_the_ID()))
        );
        $posts = get_posts($child_args);

        foreach($posts as $post){

            $date = date('Y-m-d H:i:s', $post->wpcf-datum-workshop);
            $choices[] = array('text' => $date, 'value' => $date);
        }

        $field['choices'] = $choices;

    }

    return $form;
}
#504397

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately - FTP access details are not working at this end.

Could you please resend it and please grant full rights to my wp-admin user.

#504416

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

still no luck with FTP access but I'm able to access the file using wp-admin.

Could you please check now. I can see the correct date. You can check the adjusted code in functions.php file.

#504458

That's awesome! Thanks!
What if I would want to include the time?

#504461

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well adjust your date function with H:i:s

For example:

$date = date("Y-m-d h:i:s",$x);

More info:
=> hidden link

#504496

Great. Thanks a lot!
For other users: this is what I ended up using to fill a select (Gravity Forms) with values from a custom field (child post):

add_filter('gform_pre_render_1', 'populate_posts');
function populate_posts($form){

    foreach($form['fields'] as &$field){

        if($field['type'] != 'select' || strpos($field['cssClass'], 'gravity-datum-select') === false)
            continue;

        // $posts = get_post_custom_values( 'wpcf-datum-workshop' );

        $choices = array(array('text' => 'Selecteer een datum', 'value' => ' '));

        $child_args = array(
        'post_type' => 'workshop-datum',
        'numberposts' => -1,
        'meta_key' => 'wpcf-datum-workshop',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(array('key' => '_wpcf_belongs_workshop_id', 'value' => get_the_ID()))
        );
        $posts = get_posts($child_args);

        foreach($posts as $post){

          $x = get_post_meta($post->ID,'wpcf-datum-workshop',true);


          $date = date('j F Y, G:i', $x);
          $choices[] = array('text' => $date , 'value' => $date);
        }

        $field['choices'] = $choices;

    }

    return $form;
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.