Skip to content

Instantly share code, notes, and snippets.

@jondcampbell
Created February 23, 2017 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jondcampbell/abbeb7ff89a41959b87edc14d8cb25db to your computer and use it in GitHub Desktop.
Save jondcampbell/abbeb7ff89a41959b87edc14d8cb25db to your computer and use it in GitHub Desktop.
public function prepare() {
$data = [];
$total = 0;
// Get the regions
$regions = get_terms( array(
'taxonomy' => 'naspd_company_region',
'hide_empty' => true,
) );
foreach ( $regions as $region ) {
$companies_args = array(
'post_type' => 'naspd_company',
'tax_query' => array(
'taxonomy' => 'naspd_company_region',
'field' => 'term_id',
'terms' => $region->term_id,
),
'orderby' => 'title',
'order' => 'ASC',
);
// Get the companies in the region
$companies_query = new \WP_Query( $companies_args );
$companies = [];
foreach ( $companies_query->posts as $company ) {
$company_address_city = get_post_meta( $company->ID,'naspd_address_city', true );
$company_address_state = get_post_meta( $company->ID,'naspd_address_state', true );
$company_address_country = get_post_meta( $company->ID,'naspd_address_country', true );
$company_address = $company_address_city . ', ' . $company_address_state . ' ' . $company_address_country;
// Add the company
$companies[] = array(
'title' => $company->post_title,
'items' => array(
'title' => $company_address,
),
);
}
$region_total = count( $companies );
$total = $total + $region_total;
// Add the region
$data['items'][] = array(
'title' => $region->name,
'items' => $companies,
'footer' => 'Total for ' . $region->name . ' Region: ' . $region_total,
);
}
$data['footer'] = 'Total for All Regions: ' . $total;
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment