Вы можете воспользоваться ассоциативными массивами PHP, чтобы гарантировать уникальность.
Вы должны внести следующие изменения:
// array_push($post_data, $values);
$post_data[ $cat_imploded ] = $values;
// return $post_data;
return array_values($post_data);
Таким образом, код get_categories_map()
должен выглядеть следующим образом:
function get_categories_map( $data ) {
// get the posts type loja
$posts_list = get_posts( array(
'post_type' => 'loja',
'posts_per_page' => 50,
'post_status' => 'publish'
));
$post_data = array();
foreach( $posts_list as $posts) {
$post_id = $posts->ID;
$post_title = $posts->post_title;
$post_estacao = wp_get_post_terms($post_id, 'estacao', array("fields" => "names"));
$post_cat = wp_get_post_terms($post_id, 'categoria_loja', array("fields" => "names"));
$cat_imploded = implode(', ', $post_cat);
$values = array(
'id' => $cat_imploded,
'title' => $cat_imploded,
'color' => $cat_imploded,
);
if ($post_estacao[0] == $linha_select && $cat_imploded != NULL ) {
// Build an associative array to ensure that only one instance of $cat_imploded exists in the array
$post_data[ $cat_imploded ] = $values;
}
}
// Use array_values() to convert the associative array into an indexed array
return array_values($post_data);
}