skip to Main Content

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


  1. 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 with WP_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:

    <?php
    // Get the 'germany' term ID from the 'job-country' taxonomy
    $germany_term = get_term_by('slug', 'germany', 'job-country');
    $germany_term_id = $germany_term->term_id;
    
    // Now, use the term ID to filter 'job-category' terms
    $terms = get_terms([
        'taxonomy' => 'job-category',
        'hide_empty' => true,
        'orderby' => 'count',
        'order' => 'DESC',
        'meta_query' => [
            [
                'key' => 'job-country', // This is assuming you have a custom field for 'job-country'
                'value' => $germany_term_id,
            ],
        ],
    ]);
    
    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 exist
        foreach ($terms as $category) { ?>
            <h2 class="category-title">
                <?= $category->name ?>
            </h2>
        <?php endforeach;
    }
    ?>
    

    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.

    Login or Signup to reply.
  2. 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:

    if ($terms) {
        $specific_terms = array();
    
        foreach ($terms as $category) {
            $term_has_relationship = false;
            $country_relationship = get_term_meta($category->term_id);
            foreach($country_relationship as $key=>$val){ 
                if ($val[0] === 'Germany') {
                    $term_has_relationship = true;
                }
            }
            if ($term_has_relationship) {
                // If the term is related to Germany, add it to the array
                $specific_terms[] = $category;
            }
        }
        // Now, you have the terms of 'job-category' that have a relationship with 'Germany' in 'job-country'
        // Loop through the specific terms
        foreach ($specific_terms as $category) {
            ?>
            <h2 class="category-title">
                <?= $category->name ?>
            </h2>
      <?php endforeach;endif;
      ?>
    

    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:

    // Enable debug mode
    define('WP_DEBUG', true);
    // Store logs in /wp-content/debug.log
    define('WP_DEBUG_LOG', true);
    

    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,

    define( 'WP_DEBUG_DISPLAY', false );
    

    this will switch off the error logging on the front-end and only log them in the debug.log file.

    Login or Signup to reply.
  3. 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:

    <?PHP
    $args = array(
        'post_type' => 'your_custom_post_type', // Replace with your post type 
    if not 'post'
        'tax_query' => array(
            array(
                'taxonomy' => 'job-country',
                'field' => 'slug',
                'terms' => 'germany'
            ),
        ),
        'fields' => 'ids' // only return post IDs
    );
    $query = new WP_Query($args);
    $post_ids = $query->posts;
    
    $terms = get_terms([
        'taxonomy' => array('job-category'),
        'hide_empty' => true,
        'orderby' => 'count',
        'order' => 'DESC',
        'object_ids' => $post_ids
    ]);
    
    usort($terms, ... );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search