8

How to get term by custom term meta and taxonomy or how to filter tax_query by term meta instead slug/id?

function custom_pre_get_posts($query)
{
    global $wp_query;

    if ( !is_admin() && is_shop() && $query->is_main_query()  && is_post_type_archive( "product" )) 
    {
        $term = ???get_term_by_meta_and_taxonomy???('custom_meta_term','my_taxonomy');
        $t_id = $term['term_id'];
        $tax_query = array
        (
            array
            (
                'taxonomy' => 'my_taxoomy',
                'field' => 'id',
                'terms' => $t_id
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'custom_pre_get_posts' );

3 Answers 3

22

Try This:

$args = array(
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
    array(
       'key'       => 'feature-group',
       'value'     => 'kitchen',
       'compare'   => 'LIKE'
    )
),
'taxonomy'  => 'category',
);
$terms = get_terms( $args );
3
  • 1
    This should be the accepted answer as this is the best practice for this case.
    – Jeremy
    Commented Apr 25, 2019 at 14:39
  • 1
    Agreed, I've confirmed and this accomplishes retrieving a term via a term meta key and value. Ensure hide_empty is set to false if you're term has no associated posts yet. Commented Jan 29, 2020 at 15:42
  • Also think this should be the accepted answer for this case
    – Tami
    Commented Aug 1, 2023 at 22:48
3

Building on the answer from ilgıt-yıldırım above, both the get_term_meta statement and $key == 'meta_value' statements need to contain $term>term_id.

Here's a complete example including the custom $wp_query request:

$term_args = array( 'taxonomy' => 'your-taxonomy' );
$terms = get_terms( $term_args );

$term_ids = array();

foreach( $terms as $term ) {
    $key = get_term_meta( $term->term_id, 'term-meta-key', true );

    if( $key == 'term-meta-value' ) {
        // push the ID into the array
        $term_ids[] = $term->term_id;
    }
}

// Loop Args
$args = array(
'post_type' => 'posts',
'tax_query' => array(
    array(
        'taxonomy' => 'your-taxonomy',
        'terms'    => $term_ids,
         ),
    ),
);

// The Query
$featured = new WP_Query( $args );
2

You'll need to loop through each of the terms in your main query conditional. Assuming there will likely be more than one term with the custom data, you'll then need to pass an array of IDs into your tax query.

For example, looping through each term to check for custom meta:

$term_args = array(
    'taxonomy' => $taxonomy_name,
    );


$terms = get_terms( $term_args );

$term_ids = array();

foreach( $terms as $term ) {
    $key = get_term_meta( $term->ID, 'meta_key', true );
    if( $key == 'meta_value' ) {
        // push the ID into the array
        $term_ids[] = $term->ID;
    }
}

Then you end up with the variable $term_ids which contains an array of the term IDs you're looking for. You can pass that to your tax query.

2
  • 1
    get_term_meta( $term->ID, 'meta_key', true ); should be get_term_meta( $term->term_id, 'meta_key', true ); as $term would be WP_Term object. Commented Aug 25, 2017 at 10:11
  • For any new comers, this is very inefficient and would be considered a last option. See the meta_query answer as this is the better approach.
    – Jeremy
    Commented Apr 25, 2019 at 14:38

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.