Hi Community I've some events (normal posts) which I want to populate in a drop down field. I could achieve that with the following code https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/#kb-populate-a-field-with-wordpress-posts. My dropdown looks now like this:
Event 1
Event 2
.... Now my question if it's possible to show also the date (custom field created with ACF) in this dropdown field after the title? that it looks something like this:
Event 1 | 26.01.2023
Event 2 | 03.03.2023
.... Here's my current code (where I use also the meta_key 'datum' to filter my posts):
add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2); function frm_populate_posts($values, $field){ if($field->id == 20){ //replace 125 with the ID of the field to populate $today = date('Ymd'); $posts = get_posts( array( 'post_type' => 'post', 'post_status' => array( 'publish'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'datum', 'compare' => '>=', 'value' => $today, ) ), )); unset($values['options']); foreach($posts as $p){ $values['options'][$p->ID] = $p->post_title; } // $values['use_key'] = true; //this will set the field to save the post ID instead of post title unset($values['options'][0]); } return $values; }
Thanks for your help :-)
add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2); function frm_populate_posts($values, $field){ if($field->id == 20){ $today = date('Ymd'); $posts = get_posts( array( 'post_type' => 'post', 'post_status' => array( 'publish'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'datum', 'compare' => '>=', 'value' => $today, ) ), )); unset($values['options']); foreach($posts as $p){ $postid = $p->ID; $eventdate = get_field('datum', $postid); $values['options'][$p->ID] = $p->post_title . " | " . $eventdate; } unset($values['options'][0]); } return $values; }Thanks for your help :-)
Hi Victor
Thanks a lot. I tried this code snipped but I don't get any value:
foreach($posts as $p){
$eventdate = the_field('datum');
$values['options'][$p->ID] = $p->post_title . " | " . $eventdate;
}
I changed it for testing to a fix value and then it shows the value in the dropdown. Just the values from the field "datum" are empty. Do I make something wrong? :-/
Aren't you supposed to pass the post_id as a parameter to the ACF function with the field name? I'm not an ACF expert. You need to consult their documentation.
I can add a post id in the code and then it shows the value of this specific post (in my case for my 4 events it shows 4 times the date of this single event). so it looks like its getting the values from posts with this code. but in the foreach they are empty...
Please login or Register to submit your answer