This is because you’re using a function to get the terms, but writing the meta_query
for the posts — the meta_query
will actually target terms instead.
A couple of ways to solve this are:
- A custom SQL query
- Get all the posts from today, then loop through them and count up the use of each term
A custom SQL query is going to look something like this:
SELECT DISTINCT wp_terms.term_id, wp_terms.name, COUNT( wp_terms.term_id )
FROM wp_terms
JOIN wp_posts
ON wp_posts.post_type="events"
AND wp_posts.post_status="publish"
JOIN wp_postmeta AS meta
ON meta.post_id = wp_posts.id
AND meta.meta_key = 'event_start_date'
AND meta.meta_value = DATE_FORMAT(CURDATE(), '%Y%m%d')
JOIN wp_term_relationships AS rel
ON rel.object_id = wp_posts.id
WHERE rel.term_taxonomy_id = wp_terms.term_id
GROUP BY wp_terms.term_id
ORDER BY wp_terms.name ASC;
That grabs all the posts you want, with the appropriate meta key and values, then grabs the associated terms with those posts. Finally, it gets one result per term and counts up how many times that term appears.
The meta_value
part could be off since I can’t test that. (I just realized I also left off the specific taxonomy as well, oops. Let me know if you’d find that helpful.) If you aren’t comfortable with SQL queries in WordPress, this is a good article, or you could opt for either the other solution mentioned, or one of your own devising!