2

Im trying to build a taxonomy query to only display the terms that have a certain custom field value assigned to it. The custom field holds a boolean value, so I tried:

$args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
        'meta_key' => 'featured',
        'meta_value' => true
    );

Also:

$args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
        'meta_query' => array(
             array(
                'key'       => 'featured',
                'value'     => true,
                'compare'   => '='
             )
        )
    );

None of these returned the expected results. What am I missing?

1 Answer 1

12

Inserting boolean term meta values

When we add non-existent term meta with e.g.

add_term_meta( 123, 'test', true );

then we are actually running the following insert :

$wpdb->insert( 'wp_termmeta', array(
   'term_id' => 123,
   'meta_key' => 'test',
   'meta_value' => true
) );

within the general add_metadata() function.

Now wpdb::insert() is actually a wrapper for wpdb::_insert_replace_helper() that prepares the SQL insert query and maps the insert values to:

INSERT INTO `wp_termmeta` (`term_id`, `meta_key`, `meta_value`) VALUES (%d, %s, %s)

or in our test case:

INSERT INTO `wp_termmeta` (`term_id`, `meta_key`, `meta_value`) VALUES (123, 'test', '1')

Also note that the meta_value column is of longtext type in the wp_termmeta table.

So the boolean true is stored as the '1' string.

Fetching boolean term meta values

When get_terms() runs with this kind of meta query:

$args = array(
    'taxonomy'   => 'product_cat',
    'hide_empty' => false,
    'meta_query' => array(
         array(
            'key'       => 'featured',
            'value'     => true,
            'compare'   => '='
         )
    )
);

then the generated SQL query contains:

wp_termmeta.meta_key = 'featured' AND wp_termmeta.meta_value = '1' 

where the true (bool) is converted to the '1' (string).

5
  • I still can't make query return any result, using a string "1" as the value passed to the meta_query but i'll keep looking. Thank you so much for the detailed explanation though. Commented Jul 4, 2016 at 19:07
  • The true meta value in the get_terms() query is also changed to '1' so I think you must be saving the term meta values differently. How do you store it? What kind of code? Are you mabye storing it as the 'true' bool-string? @LuisMartins
    – birgire
    Commented Jul 5, 2016 at 9:17
  • 1
    Any progress on the problem? @LuisMartins
    – birgire
    Commented Jul 23, 2016 at 13:11
  • Sorry I totally missed SO notifications. Commented Aug 13, 2016 at 13:04
  • No problem, was just curious about it ;-) @LuisMartins
    – birgire
    Commented Aug 13, 2016 at 13:07

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.