1

I have a lot of scheduled posts (location) in my db, and i want to show only the taxonomies (locationtype), which have published posts.

therefore i created a check_term_posts($term_id) function

but it always returns 0.

WP Query seems to be the only way to accomplish that, as $term->count seems to deliver everything.

    protected function check_term_posts($term_id) {
            $args = [
                'posts_per_page' => -1,
                'post_type' => 'location',
                'post_status' => 'publish',
                'tax_query' => [[
                    'taxonomy' => 'locationtype',
                    'field' => 'term_id',
                    'terms' => $term_id
                ]]
            ];

            $q = new \WP_Query($args);
            return $q->post_count;
    }

$q->post_count is always zero

DudiDude
  • 355
  • 3
  • 12
  • See this one https://stackoverflow.com/questions/12250957/wordpress-query-posts-taxonomy?rq=1 . It is using 'id' field instead of 'term_id' for taxonomy. – Sagar Bahadur Tamang May 16 '19 at 08:44

1 Answers1

1

you have to replace

'field' => 'term_id', to 'field' => 'id',

your code become like this

$args = [
                'posts_per_page' => -1,
                'post_type' => 'location',
                'post_status' => 'publish',
                'tax_query' => [[
                    'taxonomy' => 'locationtype',
                    'field' => 'id',
                    'terms' => $term_id
                ]]
            ];
Owais Alam
  • 287
  • 1
  • 9
  • 13