vendredi 2 janvier 2015

Trying to Sort/Order ACF form fields


I've taken over a project and I'm a bit stuck with some code left by a previous developer. Currently he's using ACF to populate the fields of a search form. It all works as it should and I don't want to change the code too much. Here's what it currently looks like:


acf search form


These fields originate from the ACF fields plugin, he's set checkbox and select fields in a custom post like this:


acf back-end


However as you can see in the first picture the months are being populated in the wrong order (I guess by post date?) not in the original ACF order. My question is how can I modify his existing code to sort months correctly and other fields such as grade -(easy>medium>hard). Here's the code that I'm working with:



<?php
$args = array(
'post_type' => 'activities',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
?>

<?php

$data;

// Create array of posts and fields
$myposts = get_posts( $args );
foreach ( $myposts as $key => $post ) : setup_postdata( $post );

$data['locations'][] = get_field('location');
$data['activity'][] = get_field('activity_type');
$data['difficulty'][] = get_field('difficulty');

// Get months
foreach(get_field('availability') as $month) {
$data['months'][] = $month;
}

endforeach;

// Omit duplicate results
foreach($data as $key => $value) {
$data[$key] = array_unique($data[$key]);
}

wp_reset_postdata();
?>


And the html:



<!-- Search Form (Small) -->
<div class='search-form-wrap'>
<div class='search-form'>
<h2>Find a Holiday</h2>
<form role="search" action="<?php echo site_url('/'); ?>" method="get">
<!-- Choose Location -->
<select name='location'>
<option value='' disabled selected>Choose Location</option>
<?php
foreach($data['locations'] as $location) {
echo "<option value='{$location}'>{$location}</option>";
}
?>
</select>

<!-- Choose Activity -->
<select name='activity'>
<option value='' disabled selected>Choose Activity</option>
<?php
foreach($data['activity'] as $activity) {
echo "<option value='{$activity}'>{$activity}</option>";
}
?>
</select>

<!-- Choose Grade -->
<select name='grade'>
<option value='' disabled selected>Choose Grade</option>
<?php
foreach($data['difficulty'] as $difficulty) {
echo "<option value='{$difficulty}'>{$difficulty}</option>";
}
?>
</select>

<!-- Choose Month -->
<select name='month'>
<option value='' disabled selected>Choose Month</option>
<?php
foreach($data['months'] as $month) {
echo "<option value='{$month}'>{$month}</option>";
}
?>
</select>


Any help would be greatly appreciated as I'm a bit stumped by this. I've checked the original documentation for acf and if I was building from scratch I would start there.





Aucun commentaire:

Enregistrer un commentaire