## This Is How To Get The Total Number Of Results When 'Include' Array Is Given In WordPress' get_terms()

If you've ever used the `get_terms()`

function in WordPress, you may have encountered an issue with the `include`

parameter. The `include`

parameter is used to specify an array of term IDs to include in the results, but it can sometimes cause unexpected behavior when trying to get the total number of results.

In this blog post, we'll explore the problem and provide a solution that will help you get the total number of results when using the `include`

parameter.

### The Problem with `include`

in `get_terms()`

The `get_terms()`

function in WordPress is used to retrieve a list of terms from a specific taxonomy. The `include`

parameter allows you to specify an array of term IDs that you want to include in the results.

However, the issue with using the `include`

parameter is that it sets up a `WHERE`

condition in the SQL query that requires all results to be within the `include`

array. This can lead to unexpected behavior, especially when you're trying to get the total number of results.

For example, let's say you have a custom taxonomy called `coupon_store`

and you want to retrieve the first 12 featured stores from a specific location, and then the remaining featured stores from other locations. You might try something like this:

```
$term_args = array(
'taxonomy' => 'coupon_store',
'number' => 12,
'include' => $location_terms,
'meta_query' => array(
array(
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
),
)
);
$store_terms_location = get_terms( $term_args );
$term_args = array(
'taxonomy' => 'coupon_store',
'number' => 12 - count($store_terms_location), // only get as many as you still need
'exclude' => $location_terms,
'meta_query' => array(
array(
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
),
)
);
$store_terms_other = get_terms( $term_args );
// Merge the two arrays together in order of priority
$store_terms = array_merge($store_terms_location, $store_terms_other);
```

This code works fine for getting the terms, but the issue is that the `include`

parameter doesn't allow you to easily get the total number of results. The `get_terms()`

function will only return the number of terms that match the `include`

array, not the total number of terms that match the other criteria (in this case, the `meta_query`

condition).

### The Solution

To solve this problem, we can use a two-step approach:

- First, we'll use the
`include`

parameter to get the initial set of terms, but we'll also keep track of the number of terms we've retrieved.
- Then, we'll use the
`exclude`

parameter to get the remaining terms, and we'll adjust the `number`

parameter to only retrieve the remaining terms we need.

Here's the code:

```
// Step 1: Get the initial set of terms using the 'include' parameter
$term_args = array(
'taxonomy' => 'coupon_store',
'number' => 12,
'include' => $location_terms,
'meta_query' => array(
array(
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
),
)
);
$store_terms_location = get_terms( $term_args );
$total_terms_retrieved = count($store_terms_location);
// Step 2: Get the remaining terms using the 'exclude' parameter
$term_args = array(
'taxonomy' => 'coupon_store',
'number' => 12 - $total_terms_retrieved, // only get as many as you still need
'exclude' => $location_terms,
'meta_query' => array(
array(
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
),
)
);
$store_terms_other = get_terms( $term_args );
// Merge the two arrays together in order of priority
$store_terms = array_merge($store_terms_location, $store_terms_other);
// Get the total number of results
$total_results = count($store_terms);
```

In this solution, we first use the `include`

parameter to get the initial set of terms. We also keep track of the number of terms we've retrieved using the `$total_terms_retrieved`

variable.

Then, in the second step, we use the `exclude`

parameter to get the remaining terms. We adjust the `number`

parameter to only retrieve the remaining terms we need, which ensures that we don't end up with more than 12 results in total.

Finally, we merge the two sets of terms together and get the total number of results using the `count()`

function.

This approach allows you to get the total number of results, even when using the `include`

parameter in the `get_terms()`

function.

### Conclusion

The `include`

parameter in the `get_terms()`

function can be a bit tricky to work with, especially when you're trying to get the total number of results. However, by using a two-step approach and combining the `include`

and `exclude`

parameters, you can easily overcome this challenge and get the total number of results you need.

Remember, this solution is not limited to the specific example we used in this blog post. You can adapt it to your own use case and apply it to any situation where you need to get the total number of results when using the `include`

parameter in `get_terms()`

.

If you're building a website or application that relies on WordPress' taxonomies and terms, understanding how to work with the `get_terms()`

function and its various parameters is crucial. Hopefully, this blog post has provided you with a useful solution that you can apply to your own projects.

For more information on WordPress development and best practices, be sure to check out Flowpoint.ai, a web analytics platform that can help you identify and fix technical issues that are impacting your website's conversion rates

### Get a Free AI Website Audit

Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.