This is untested, but you have do an nested loop where you loop parent categories and child categories. When you find an correct parent you loop the posts and check which posts have the correct category.
<?php
// Fetch categories in to an array
// Fetch provinces in to an array
// First loop your categories
foreach ( $categories as $category ) {
printf( '<h2>%s</h2>', $category->name );
// If we have provinces
if ( ! empty( $provinces ) ) {
// Let's loop all the provinces
foreach ( $provinces as $province ) {
// Does the province have an correct category
if ( $province->category_parent === $category->term_id ) {
printf( '<h3>%s</h3>', $province->name );
// Loop your posts
foreach ($posts as $post ) {
// Does the post have correct province
if ( has_category( $province, $post ) ) {
// Print your dealer info here. Ideally you would use get_template_part() function
// To avoid duplication
}
}
}
}
} else {
// Loop your posts
foreach ($posts as $post ) {
// Doest the post have correct category
if ( has_category( $category, $post ) ) {
// Print your dealer info here. Ideally you would use get_template_part() function
// To avoid duplication
}
}
}
}