Query Database for all ACF repeater Datepickers and list them in order by date

This sample will query the database for the Custom Field of “birth”, a repeater date picker setup to show a persons birthday. It will pull all birthdays in the month of June and list them in order. This example will also pull the name field that is in the same repeater by referencing the postmeta ID of the row. This was adapted from the example on the ACF website.

You may have to change your table prefix and your field names.

get_results($wpdb->prepare( 
            "
            SELECT * 
            FROM we_postmeta
            WHERE meta_key LIKE %s
               AND meta_value LIKE %s
		ORDER BY meta_value ASC;
			",
            'repeater_%_birth', // meta_name: $ParentName_$RowNumber_$ChildName
            "____06__" // meta_value:
		 ));
		
	// loop through the results
	if( $rows )
	{
		foreach( $rows as $row )
		{
// for each result, find the 'repeater row number' and use it to load the info!
	preg_match('_([0-9]+)_', $row->meta_key, $matches);
			
// Load the birth dates from the repeater field
	$meta_key = 'repeater_' . $matches[0] . '_birth'; // $matches[0] contains the row number!
			
// Get the names from the repeater field
	$names = 'repeater_' . $matches[0] . '_name'; // $matches[0] contains the row number!
 
//  use get_post_meta to load the info
// - http://codex.wordpress.org/Function_Reference/get_post_meta
	$birth_id = get_post_meta( $row->post_id, $meta_key, true );
	$name = get_post_meta( $row->post_id, $names, true );
// Delet the year
	$finalFormat = DateTime::createFromFormat('Ymd', $birth_id);
?>
			
       

format('M d'); ?>

Note: if you wanted to pull the current month, you could pass a php variable in to the query.

$currentMonth = date('m'); 

$rows = $wpdb->get_results($wpdb->prepare( 
            "
            SELECT * 
            FROM we_postmeta
            WHERE meta_key LIKE %s
               AND meta_value LIKE %s
		ORDER BY meta_value ASC;
			",
            'repeater_%_birth', // meta_name: $ParentName_$RowNumber_$ChildName
            "____${currentMonth}__" // meta_value passed from php variable
		 ));

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = 2