Filter "get_terms" query

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?

Topic terms taxonomy custom-taxonomy Wordpress

Category Web


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.


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 ..."

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.