Populate posts to dorpdown with meta values

By: Fabian Suter | Asked: 01/26/2023
ForumsCategory: Code HelpPopulate posts to dorpdown with meta values
Fabian Suter asked 2 years ago

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 :-)

Attachments
2 Answers
Best Answer
Fabian Suter answered 2 years ago
Hi Victor Got it. You brought the good idea with the post ID. I had to change it to the acf command get_field and then the loop was working :-) Here the final code:
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 :-)
Victor Font answered 2 years ago
For each of the posts, you'll need to retrieve the ACF field and concatenate it to the dropdown label. Insert the ACF function after the foreach and change $values['options'][$p->ID] = $p->post_title; to $values['options'][$p->ID] = $p->post_title . "|" . $my_acf_field;. See this: https://www.advancedcustomfields.com/resources/the_field/
Fabian Suter replied 2 years ago

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? :-/

Victor Font replied 2 years ago

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.

Fabian Suter replied 2 years ago

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...

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right