Reputation: 1723
I want to get 12 term objects by calling get_terms
; and I have a specific array of terms_ids
which should come first.
Here is my query so far:
$term_args = array(
'taxonomy' => 'coupon_store',
'number' => 12,
'include' => $location_terms,
'meta_query' => array(
array(
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
),
)
);
$store_terms = get_terms( $term_args );
The problem is that since $location_terms
consists of only 3 values, the whole result count is also restricted to 3. I know this is not quite possible as per get_terms documentation.
Is there any hack to get the rest 9 results, after getting the 3 from that array?
UPDATE
I have achieved it by using 2 queries as described in @Yoda's answer. Is there any way to get it done by using get_terms
only once.
Upvotes: 0
Views: 3279
Reputation: 47665
What you are trying to do doesn't make a whole lot of sense.
If you already know the identity of the three terms you to have get first in your array, making a query to get those is kinda irrelevant. You could simply make two separate queries and merge the results.
E.g.:
$first_terms = array(
'taxonomy' => 'coupon_store',
'include' => $location_terms,
);
$store_terms_1 = get_terms( $term_args );
$remaining_terms = array(
'taxonomy' => 'coupon_store',
'number' => 9,
'exclude' => $location_terms,
'meta_query' => array(
array(
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
),
)
);
$store_terms_2 = get_terms( $term_args );
$resulting_terms = array_merge( $store_terms_1, $store_terms_2 );
Without knowing more about your data, it is not easy to do much more.
Guesstimating a bit, since you are already using term metas in your query, you could add another term meta with the order, and use that to order your results. This way you wouldn't need to hardcode those first three terms that you want to have on top, and you would only need to make one query.
E.g.:
$store_terms = [
'taxonomy' => 'coupon_store',
'number' => 12,
'meta_key' => 'coupon_store_term_order,
'orderby' => 'meta_value_num',
'meta_query' => [
[
'key' => '_wpc_is_featured',
'value' => 'on',
'compare' => '=',
],
]
];
This way, you have to set up a new term meta for your terms (coupon_store_term_order
), and save there your desired order. You would need a bit more fiddling (e.g. deal with terms with no defined order, etc).
And logically, I'm further assuming that these three terms are also featured terms. Otherwise, making two requests and merging is still the only logical way to go.
Upvotes: 1
Reputation: 1132
So... this basically doesn't work.
The include
parameter sets up a where condition in the SQL query that requires all results to be within the include
array.
So, that's not great for what you're after.
However, I do have a solution for you!
$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);
So, to cover what this does:
This should give you the results you need. You can tidy it up, use some conditionals to determine whether the latter part needs to run, etc. Build on the general idea and make it fit what you're trying to do with your code.
Upvotes: 1