I've added some code to my templates which automatically adds custom fields to cache some data that each post pulls in. Is there a way that I can track how many posts have had this custom field set?
You could run a custom query to count the amount of posts with a given key using
Echo or store the value returned as needed. If you're intending on passing in any data that's been requested or sent by a user, then you'll need to add some further data checking inside the function.
NOTE: If you're doing this counting on an admin page, then there may be an internal function you can use instead(i can't remember specifically which right now, but i'll look it up if you need something admin side).
Hope that helps in any case.
Addition: I was curious after Viper posted to see how much difference there would be between the two queries, so i did each call 5 times, and recorded the time..
I would note, when
Think my results are flawed? Please do perform your own tests, and should you want a copy of the code i used, you can find it in barebones form here.
Viper does have a valid point(future proofing is always a good thing), i just wanted to show there are some measurable differences between the two methods(and if anyone sees a problem with my approach, please feel free to let me know).
I'd recommend using get_posts(). It'll use up more memory than directly querying the database as it'll be returning the full post objects, but it has the advantage of being essentially future proof as you're using the API.
See the query_posts() docs for a full list of arguments that you can use and a ton of great examples.