How to make a selection of tags of all posts, excluding all repetitions? I need to display 1 or more search tips, and now it is displayed equal to the number of posts. How to fix? Now the code looks like this:

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' ); 

Here is the output: image

Live to try here .

    1 answer 1

    To display the tags, it is not necessary to go through all the posts (which is slow). Use the wp_tag_cloud() function - it will output a tag cloud, with a limit on the number, with links to these tags, with different font sizes depending on the frequency with which the tags are used.

    It looks like this

    enter image description here

    Details in Russian here .

    • I don’t work with this function at all, I updated the code in question, now the result is better, but still not what is needed. - spoilt
    • @spoilt if "not working" means something you are not doing. This is the correct solution, unlike get_posts for displaying tags. - SeVlad
    • @SeVlad I don't need to output them, just save the list of all tags to an array for custom record types - spoilt
    • @spoilt, and the description of the function niasililil? Well and except it is, etc. get_tags napr. - SeVlad