0

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?

3 Answers 3

0

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 ..."
1
  • That looks nasty af. Sorry but it wouldn't happen. Commented Apr 26, 2019 at 21:04
0

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.

0

In WordPress, the get_terms function is designed to return a full WP_Term object for each term, which includes all the properties you've mentioned like term_id, name, slug, term_group, etc. Unfortunately, there isn't a direct way to limit the get_terms function to only return specific fields like name and slug.

However, you can achieve your goal by fetching all terms with get_terms and then extracting only the data you need. This approach involves additional processing in PHP but is straightforward to implement. Here's how you can do it:

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

// Extract only the name and slug
$filtered_terms = array_map(function($term) {
    return array(
        'name' => $term->name,
        'slug' => $term->slug
    );
}, $terms);

// Now, $filtered_terms contains only the name and slug of each term

In this code snippet, array_map is used to iterate over each term and create a new array containing only the name and slug properties.

While this does not reduce the data initially fetched from the database, it does provide you with an array that only contains the information you need. This can be particularly useful if you're passing this data to a front-end application or API where minimizing the amount of transmitted data is crucial.

If you're looking to optimize database queries for performance reasons, and the dataset is large, you might consider custom SQL queries, but that's usually not recommended due to maintenance and compatibility issues with WordPress core updates. For most typical use cases, the above method should be sufficient and keeps your code within the WordPress API framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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