I’m trying to figure out one topic, I have the impression that the solution is child’s play, but somehow I don’t know how to end it. To the point – I have code related to get_terms:
<?PHP
$terms = get_terms([
'taxonomy' => array('job-category'),
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
]);
usort( $terms, function( $a, $b ) {
$a_ste = (int) get_term_meta( $a->term_id, 'stick_to_end', true );
$b_ste = (int) get_term_meta( $b->term_id, 'stick_to_end', true );
if ($a_ste == $b_ste) return 0;
return ($a_ste < $b_ste) ? -1 : 1;
} );
if ($terms) { //categories exists
foreach ($terms as $category) { ?>
<h2 class="category-title">
<?= $category->name ?>
</h2>
<?PHP endforeach; endif; ?>
Everything works ok. I fetch the terms from the job-category taxonomy and display them.
The issue is that I want these displayed job-category terms to be only those that have very specific terms ("germany") given in a different taxonomy (job-country). How to get it? in other loops like wp_query I mostly used tax_query. But here I need/want to stick to get_terms…
So just to sum up – I’m looking for a way to use get_terms with:
- job-category (all terms)
- job-country (only term "germany")
Thank you!
3
Answers
To retrieve only the ‘job-category’ terms that are associated with the ‘germany’ term in the ‘job-country’ taxonomy using
get_terms
, you can’t directly filter by taxonomy terms as you would withWP_Query
. However, you can achieve this by first getting the ‘germany’ term ID from the ‘job-country’ taxonomy and then using it to filter the ‘job-category’ terms. Here’s how you can do it:In this code, we first obtain the ‘germany’ term ID from the ‘job-country’ taxonomy and then use it in the
meta_query
parameter to filter the ‘job-category’ terms. This way, you’ll only get ‘job-category’ terms that are associated with ‘germany’ in the ‘job-country’ taxonomy. Make sure to replace'job-country'
and'job-country'
with your actual taxonomy and custom field names as needed.Try creating a loop within the conditional for the terms in your code, similar to the one below, to specify the job-country just below the
usort()
. For example:From what I understand of your question, I think this would help.
You can also debug the code to analyze the responses more effectively in WordPress.
Open the wp-config.php file. And insert the following code into the file:
In the above code, in addition to enabling debugging, all encountered errors are also stored in a log file. The file is located at wp-content/debug.log.
If you are working on a live installation, or your code is reporting a lot of bugs, you should also set the following to false in your wp-config.php file,
this will switch off the error logging on the front-end and only log them in the debug.log file.
To get terms from (job-category) based on a term from (job-country), you have to see which posts are tagged with that specific term and then find out which job-category terms are assigned to those posts.
Use WP_Query with object_ids in get_terms to get the post IDs, you can utilize the object_ids parameter in get_terms to fetch the job-category terms associated with these posts.
For example: