I have a big categories tree, around 2000 subcategories, and I have a lot of posts with a custom field city (different values).

I have created filter posts by "city" field. But I have a problem with categories list. I just need to show all categories that have 1+ posts with needed value in the city field.

If I get all categories in an array and check the number of posts by the custom field in each category in a loop - that will create thousands of DB queries. I need a more accurate solution to do this.

1 Answer 1


try this one..

<div style="display:<?php $meta_city = get_post_meta( get_the_ID(), 'city', true );
            if ($meta_sub_img == '') {
                echo 'none';
            } else {
                echo 'block';

<!--- your content --->


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.