How to filter categories by custom field?

You should be able to use the meta_query attribute that uses WP_Meta_Query behind the scenes:

$child_categories = get_categories(
    array(
        'parent'     => $cat_id,
        'orderby'    => 'id',
        'order'      => 'DESC',
        'hide_empty' => '0',
        'meta_query' => array(
            array(
                'key'     => 'mykey',     // Adjust to your needs!
                'value'   => 'myvalue',   // Adjust to your needs!
                'compare' => '=',         // Default
            )
        )
    )
);

This assumes the term meta data to be stored in the term meta table, e.g. added with add_term_meta() or update_term_meta(), as of WordPress 4.4.

The previous approach was to store the term meta data in the options table, as seems to be the case with the ACF plugin OP is using.

Leave a Comment