Как сделать выборку меток всех постов, исключая все повторения? Мне нужно, чтобы выводилась 1 или несколько поисковых подсказок, а сейчас выводится равное количеству постов. Как поправить? Сейчас код имеет такой вид:
function theme_enqueue_scripts() {
// Enqueue jQuery UI and autocomplete
wp_enqueue_script( 'jquery-ui-core' );
wp_enqueue_script( 'jquery-ui-autocomplete' );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );
function theme_autocomplete_js() {
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => -1 // all posts
);
$posts = get_posts( $args );
if( $posts ) :
foreach( $posts as $k => $post ) {
$source[$k]['ID'] = $post->ID;
$tags = wp_get_post_terms($post->ID, 'portfolio_entries');
foreach($tags as $tag){
$tags_array[] = $tag->name;
//$tags_array = array_unique($tags_array);
}
$tags_array = array_unique($tags_array, SORT_REGULAR);
$comma_separated_tags = implode( ', ' , $tags_array);
$source[$k]['label'] = $comma_separated_tags;//$post->post_title; // The name of the post
$source[$k]['permalink'] = get_permalink( $post->ID );
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var posts = <?php echo json_encode( array_values( $source ) ); ?>;
jQuery( '#s' ).autocomplete({
source: posts,
minLength: 2,
select: function(event, ui) {
var permalink = ui.item.permalink; // Get permalink from the datasource
window.location.replace(permalink);
}
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'theme_autocomplete_js' );
Вот результат вывода:
В живую попробовать тут.