I'm currently getting the data from a custom taxonomy using this:

$terms = get_terms(array(
    'taxonomy' => 'my_custom_taxonomy_name',
    'hide_empty' => true

Now, this returns a lot of stuff in each WP_Term object; term_id, name, slug, term_group, term_taxonomy_id, taxonomy, description, parent, count and filter.

I only need to get 2 of those: name and slug. How can I filter that query so it doesn't returns all that unused data?


The way you are doing it, you can't. You would have a do a manual query for exactly what you want.

global $wpdb;
$query = "SELECT 'name', 'slug' FROM wp-term_taxonomy WHERE ..."
  • That looks nasty af. Sorry but it wouldn't happen. Apr 26 '19 at 21:04

The "fields" parameter allows you to choose what will be returned by get_terms(). You can choose an array of names ('fields' => 'id=>names') or slugs ('fields' => 'id=>slugs'), but not an array with both properties.

There is a filter get_terms_fields, in which you can change the columns to be queried (get only name and slug), but the result will still be an array of WP_Terms objects.

I think it will be best to stay with the solution that you currently have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.