i'm trying to make a JSON route in WordPress that shows only the categories of the posts, without repeating, what i have so far its this:
add_action( 'rest_api_init', 'register_route_cat' );
function register_route_cat() {
register_rest_route(
'lojas/v1',
'/linha/(?P<stringvar>.+)',
array(
'methods' => 'GET',
'callback' => '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 ) {
array_push($post_data, $values);
}
}
return $post_data;
}
and it gives me this:
https://i.stack.imgur.com/KvHNe.png
how can i remove the repeated objects?
Thanks!