Title: get_terms with count using meta value
Last modified: February 2, 2017

---

# get_terms with count using meta value

 *  [candell](https://wordpress.org/support/users/candell/)
 * (@candell)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/get_terms-with-count-using-meta-value/)
 * I created a custom post type (jobs), with taxonomies (category, salary range 
   etc) for a jobs board. Each post has a meta key of closing_date to specify the
   date the post is closing.
 * I am modifying the query for the jobs pages to only feature jobs that are published
   and before the closing data meta key value.
 *     ```
       add_filter( 'pre_get_posts', 'jobs_query_for_beautiful_filters' );
       function jobs_query_for_beautiful_filters( $query ) {
       $timecutoff = date("Ymd");
   
       // Do nothing if not on Job page, we only want to effect this query not all!
       if ( (! is_post_type_archive('sc_jobs') ) && ( ! is_tax( 'job_categories' ) ) ) {
          return;
       }
   
       $query->set( 'post_type', 'sc_jobs' );
       $query->set( 'orderby', 'meta_value' );
       $query->set( 'meta_key', 'closing_date' );
       $query->set( 'meta_compare', '>=' );
       $query->set( 'meta_value', $timecutoff );
       $query->set( 'order', 'ASC' );
       }
       ```
   
 * *I am using the plugin Beautiful Taxonomy filters to provide the search tools
   for the jobs post type.
 * On the archive and taxonomy pages, I show a list of the categories and ideally
   the number of jobs within that taxonomy
 *     ```
       <ul>
       <?php 
   
       $terms = get_terms('job_categories'); 
       if ( !empty( $terms ) && !is_wp_error( $terms ) ){ 
         echo '<ul>'; 
   
         foreach ( $terms as $term ) { 
         $term = sanitize_term( $term, 'job_categories' ); 
         $term_link = get_term_link( $term, 'job_categories' ); 
   
         echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '&nbsp;(' . $term->count . ')' . '</a></li>'; 
   
       } 
       echo '</ul>';
       }
   
       ?>
       </ul>
       ```
   
 * My problem is that the count is displaying all published jobs and ignoring the
   closing_date meta key.
 * Is it possible use the meta key?
 * Thanks

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/get_terms-with-count-using-meta-value/#post-8731647)
 * Meta keys are fine, but $term->count contains the total post count from the DB
   table, not the count related to your restricted query. To get an accurate count,
   you need to do a restricted query for each term. Then the count will be in the
   query’s $post_count property.
 * In large databases, all these queries will be very time consuming. You could 
   likely speed things up by making custom queries through the global $wpdb object.
   Using the SQL_CALC_FOUND_ROWS keyword should speed things up even more.

Viewing 1 replies (of 1 total)

The topic ‘get_terms with count using meta value’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 5 months ago](https://wordpress.org/support/topic/get_terms-with-count-using-meta-value/#post-8731647)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
