简体   繁体   中英

Get Custom Custom Taxonomy Terms with a Count of Duplicates

I've got a custom wordpress query of a custom post type with a custom taxonomy. I'm trying to echo out a list of linked terms (tags). For the duplicated terms, I'd like to show the count of duplicated terms. Unfortunately I can't figure out how to count the items & store the info before I call array_unique(). Any suggestions would be greatly appreciated. Here's my Wordpress Query:

//Begin Custom Query
    $args=array(
      'post_type' => 'gw_activity',
      'post_status' => 'publish',
      'orderby' => 'date',
      'meta_query' => array(
         'relation' => 'AND',
          array(
             'key' => 'activity_category',
             'value' => 'mindful_life',
             'compare' => '='
          )
       ), 
      'posts_per_page' => -1
    );
    $my_query = null; 
    $my_query = new WP_Query($args);

Here's how I'm building my array of the terms:

    if( $my_query->have_posts() ) { 
     $all_terms = array();
     $all_slugs = array();
      while ($my_query->have_posts()) : $my_query->the_post(); ?>      
      <?php $terms = wp_get_post_terms( $my_query->post->ID, array( 'gw_activity_tag' ) ); ?>
      <?php       
            foreach ( $terms as $term ) {
                $all_terms[] = $term->name; // Used for Display Purposes
                $all_slugs[] = $term->slug; // Used in the Final Permalink of the term
            }        
      ?>                                            
      <?php endwhile; }
      wp_reset_query();
      // End Custom Query

Here's how I'm making the unique array of terms & slugs:

     $unique_terms = array_unique( $all_terms ); // Eliminate Duplicate Terms
     $unique_slugs = array_unique( $all_slugs );    // Eliminate Duplicate Slugs

        $final_term_list = array(); //Build Array of Unique Terms
        $i = 0;
        foreach ($unique_terms as $value) { // Build a Unique Array of Terms
            $final_term_list[$i] = $value;
            $i++;
        }

        $final_slug_list = array(); //Build Array of Unique Slugs
        $s = 0;
        foreach ($unique_slugs as $slug_value) { // Build a Unique Array of Slugs
            $final_slug_list[$s] = $slug_value;
            $s++;
        }           

Here's how I'm outputting the data

     $fl = 0; //Create a counter to index the array  
     foreach($final_slug_list as $final_slug) {                        
             echo '<a href="'. get_bloginfo('url') .'/activity-category/'. $final_slug_list[$fl] .'/">'.$final_term_list[$fl].'</a>';
             $fl++;
                } // End foreach

Here is something you can try...

$count_terms = array_count_values($all_terms);
$count_slugs = array_count_values($all_slugs);

You can do this, a little bit shorter.

$all_terms = array();
$all_slugs = array();
foreach ( $myMapPosts as $post ) : setup_postdata( $post );
    $terms = wp_get_post_terms( $post->ID, array( 'listing_category' ) );    
    foreach ( $terms as $term ) {        
        $all_terms[$term->slug] = array('name' => $term->name, 'slug' => $term->slug, 'count'=>0); // Used for Display Purposes
        $all_slugs[] = $term->slug;
    } 
endforeach;

$terms = array_count_values($all_slugs);
foreach ($terms as $key => $value) {
    $all_terms[$key]['count'] = $value;
}

var_dump($all_terms);

this way you don't have to worry about delete duplicates

Results

array(3) {
  ["restaurants-cafes"]=>
  array(3) {
    ["name"]=>
    string(23) "Restaurants & Cafes"
    ["slug"]=>
    string(17) "restaurants-cafes"
    ["count"]=>
    int(20)
  }
  ["food-stores-markets"]=>
  array(3) {
    ["name"]=>
    string(25) "Food Stores & Markets"
    ["slug"]=>
    string(19) "food-stores-markets"
    ["count"]=>
    int(8)
  }
  ["other"]=>
  array(3) {
    ["name"]=>
    string(5) "Other"
    ["slug"]=>
    string(5) "other"
    ["count"]=>
    int(2)
  }
}

Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM