Support

Account

Home Forums General Issues ACF with wp-advanced-search

Unread

ACF with wp-advanced-search

  • I’m having issues using ACF in conjunction with another plugin, https://github.com/growthspark/wp-advanced-search

    I have some code for the form that takes the structure of:

    $args['fields'][] = array('type' => 'meta_key',
    		'label' => 'Location',
    		'meta_key' => 'address_city', 
    		'format' => 'select',
                     'values' => array('' => 'Any', 'city1' => 'City1', 'city2' => 'City2', etc... ), 
    		 'operator' => 'IN');

    For the purpose of the exercise, I need to auto-populate this selector with content from the custom fields. In this case, the custom field is called ‘Address City’. I’ve attempted to do this by creating a function called Cities(). This has been verified by the plugin creator as a viable method.

    $args['fields'][] = array('type' => 'meta_key',
    		'label' => 'Location',
    		'meta_key' => 'address_city', 
    		'format' => 'select',
                     'values' => Cities(), 
    		 'operator' => 'IN');

    My Cities() function looks as follows:

    function Cities() {
    
    	$args = array(
                    'post_type' => 'property',
                    'posts_per_page' => '-1'
         );
       	$inner_query = new WP_Query( $args );
        
        $temp = array(); 
    	$cities = array();
    	unset($cities);
    	$counter = 0;
    	
    	while ( $inner_query->have_posts() ) : $inner_query->the_post();
    
    			$field_key = "field_53ea04d8b96b6";
    			$post_id = get_the_ID() ;
    			$field = get_field_object($field_key, $post_id);
    			
    			$cities[$counter] =  $field['value'];
    			
    	$counter++; endwhile;
    	
    	$result = array_unique($cities);	
    	sort($result);
    	
    	foreach($result as $key => $value) {
    		$key = $value;
    		$a[$key] = $value;
        }
    	return $a;
    
    }

    I appreciate my code is messy, but I can’t get this to work properly, as the array is not being returned in the correct format. Does anyone have any ideas as to how I can return the array of all fields in the form of:

    [value] => [label]

    Thanks

Viewing 1 post (of 1 total)

The topic ‘ACF with wp-advanced-search’ is closed to new replies.