Org-mode mapping API
# org-map-entries
# Counting entries (headlines)
This is probably the most basic use case for this. I needed in publish.org to count the number of headlines. I add a table of contents export option only when there are more than 3 headlines in an org note.
Get the number of headlines
(length (org-map-entries t))
3