I needed a simple way to show the number of posts in a given post type outside of an Etch loop.
If you need to do the same, place this in your functions.php or code snippet manager.
add_filter('etch/dynamic_data/option', function ($data) {
$counts = wp_count_posts('{{your post type slug}}');
$data['{{name of the option}}'] = (int) ($counts->publish ?? 0);
return $data;
});Then you would use {options.nameOfYourOption}. You could simply use this in the builder, or use it to create a component or pattern (the pattern is used below).
add_filter('etch/dynamic_data/option', function ($data) {
$counts = wp_count_posts('articles');
$data['articleCount' = (int) ($counts->publish ?? 0);
return $data;
});There are 62 published articles.