Can you filter wp_dropdown_categories with terms meta?

Hi I have a custom taxonomy with a term meta flag called ‘is_live’ set up and I want to exclude all the terms where this flag is false when I call wp_dropdown_categories.

I’ve checked the codex but cant see anything relating to terms meta.

Read More

Anyone have any ideas or tried this before?

Related posts

Leave a Reply

1 comment

  1. There is no direct way, hence you’ll have to hack it. wp_list_categories calls get_terms internally. There’s a filter terms_clauses which will allow you to modify the WHERE conditions for getting the terms. Here’s how you can use it:

    <?php
        add_filter('terms_clauses', 'term_filter', '', 1);
    
        function term_filter($pieces){
    
            //echo "<pre>";
            //print_r($pieces);
            //echo "</pre>";
    
            //You can change the entire SQL by examining the pieces array
            $pieces['where'] .= '<SQL to get the term meta>'; 
    
            return $pieces;
        }
    
        //Here's how $pieces looks like:
        Array
        (
            [fields] => t.*, tt.*
            [join] => INNER JOIN wp313_term_taxonomy AS tt ON t.term_id = tt.term_id
            [where] => tt.taxonomy IN ('category') AND tt.count > 0
            [orderby] => ORDER BY t.term_id
            [order] => ASC
            [limits] => 
        )
    ?>
    

    Append your WHERE condition to the default WHERE condition. I’m not very good with SQL (honestly, I couldn’t find where term meta is stored!) hence didn’t write the query. For more reference you can check the file taxonomy.php (line. no. 1311) and these 2 posts: Link 1, Link 2.

    Hope it helps!