This is an ongoing request, trying to do maximize some code to display a list of Accomodation by Country, of a certain Type, and a specific Region.

So for example, a list of ALL accommodations in FRANCE, By Hotel Rating (1,2,3,4,5), and then by Region (Paris, Marseille etc...) Listing will be

-Hotel 1Star
---Hotel 1
---Hotel 2
---Hotel 1
---Hotel 2

-Hotel 2Star
etc.. etc...

Each Hotel entry is a custom post called Accomodation I have 3 taxonomies, countries (ie France), accomodation-type (ie Hotel 1 star), region (ie Paris).

The listing of accommodations "lives" on a country page, so the value of the country is already know. (ie $termcountry->name)

My code is a below:

            $taxonomy2 = 'accomodation-type';
        $termsacc = get_terms("accomodation-type",array('orderby' => 'slug', 'order' => 'ASC'));

        foreach ($termsacc as $termaccomodation) {
            $taxonomyregion = 'region';
            $termsreg = get_terms("region",array('orderby' => 'slug', 'order' => 'ASC'));
             foreach ($termsreg as $termregion) {

             $query = array(
              'post_type' => 'accomodation',
              'accomodation-type' =>$termaccomodation->name,
              'country' =>$termcountry->name, 
              'orderby' => 'title', 
              'order' => 'ASC',
              'posts_per_page' => 48,                 
              'tax_query' => array(
                    'taxonomy' => $taxonomyregion,
                    'field' => 'slug',
                    'terms' => $termregion->name,
                    'orderby' => 'title', 
                    'order' => 'ASC',



             $count = 1; //First we set the count to be zeo    
             if(have_posts() ) {  
             $termaccomodation->slug = str_replace("%e2%80%98", "", $termaccomodation->slug);
             $termaccomodation->slug = str_replace("%e2%80%99", "", $termaccomodation->slug);

              $regionname = "";
              $regionname = " - ".$termregion->name;
             $id = '';
             $new_id = ++ $id;
             <a name="<?php echo   $new_id."-".$termaccomodation->slug."-".$termregion->slug;?>"></a>
             <div id="accitem">
                <?php echo "<h3>".$termaccomodation->name."".$regionname."</h3>";?>

             while (have_posts()) : the_post();
             $linkacc = get_custom_field("AccomodationLink");
             I do my loop stuff and display the accom list.
             then close my loop
             <?php endwhile; wp_reset_query();?>

This is working, but REALLY SLOW... and I am pretty sure it's not the optimal/best way of doing it. Could I pick your brains on this? Help!

cheers s

1 Answer 1


Just answering bit by bit here and will add any optimisations I see as I go:

1) $termsreg = get_terms("region",array('orderby' => 'slug', 'order' => 'ASC')); can be run above the foreach loop. There is no need to run this however many times.

2) Are "all" your hotels that you have in your custom post type get listed somewhere in this page? If during this double loop all your hotels are at least shown once then it would be better to get all you hotels loaded first into an array along with the tax's they belong to. then in the foreach check against the data you already received and then then show the hotel rather than grabbing it from the DB each time. What I'd do to get the hotel data in the first place would be to a custom SQL query to make sure I only pulled the data I needed and to pull both associated tax's.

Thats all I come up with for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.