Delete double values using php inside an array (wp-query and ACF) -
i'm using advanced custom fields on website select field (type_evenement
) 5 possible values (val_1, val_2, val_3, val_4, val_5)
i'm using wp query on custom template page display posts category, posts uses "select" custom field.
i'm trying display select values on page, once, i'm trying delete double values, using array_unique, it's not working.
here code wrote display values inside loop, display values, when double, example, val_1, val_3, val_4, val_2, val_1, val_1...
<?php // args $today = date("ymd"); $args = array ( 'category' => 5, 'posts_per_page' => -1, 'meta_key' => 'debut_date', 'orderby' => 'meta_value_num', 'order' => 'asc', 'meta_query' => array( array( 'key' => 'fin_date', 'compare' => '>=', 'value' => $today, ) ), ); // results $the_query = new wp_query( $args ); // loop ?> <?php if( $the_query->have_posts() ): ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php $input = get_field('type_evenement'); echo($input); ?> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?>
but when using array_unique, nothing displayed anymore :
<?php $input = get_field('type_evenement'); $result = array_unique($input); echo($result); ?>
i don't understand i'm doing wrong, know get_field returns array, guess array_unique should work no ?
if can me nice !
thanks lot
$input
single value, not array. duplicates repeatedly assigned value while loop
.
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $input = get_field('type_evenement'); echo($input);
you either fix query returns duplicate, since looks wordpress, might not going work.
so fill array first, use array_unique()
, echo values (or simple not add value again, if temporary array contains it):
$myarr = array(); while ( $the_query->have_posts() ) { $the_query->the_post(); $input = get_field('type_evenement'); if (!in_array($input,$myarr)){ $myarr[] = $input; } } foreach ($myarr $value){ echo $value; //unique values. }
Comments
Post a Comment