Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is a follow up to Display Post by taxonomy and taxonomy child if exist

I need to display a list of Accomodation, first by Type of Accomodation, then by Regions. I have a post-type, called accomodation, a taxonomy Country, a taxonomy accomodation-type, a taxonomy Region

I am in the Country page, where I retrieve the value of the country via

$taxonomy = 'country';
$terms=get_the_terms($post->ID,$taxonomy);

if($terms) {
    foreach( $terms as $termcountry ) {

So I am successfully getting $termcountry->name; as the value of current country.

Next I need to do my query for the Accomodation based on that country, filtered by Accomodation Type, and by region in this type. So it should be

  • Acc-Type 1
    • Region1
      • Acco 1
      • Acco 2
      • Acco 3
    • Region 2
      • Acco 1
      • Acco 2
  • Acc-Type 2
  • etc....

I am trying to do it this way

$taxonomy2 = 'accomodation-type';
$termsacc = get_terms("accomodation-type",array('orderby' => 'slug', 'order' => 'ASC'));
       //Loop through Acc-Type
        foreach ($termsacc as $termaccomodation) {
            //Loop through Region in this type
            $taxonomyregion = 'region';
            $termsreg = get_terms("region",array('orderby' => 'slug', 'order' => 'ASC'));
             foreach ($termsreg as $termregion) {
            $args = array(
                'post_type' => 'accomodation',
                'country' => $termcountry->name,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'accomodation-type',
                        'field' => 'slug',
                        'terms' => array($termaccomodation->name),
                    ),
                    array(
                        'taxonomy' => 'region',
                        'field' => 'slug',
                        'terms' => array($termregion->name),
                    )
                )
            );


            $query = new WP_Query($arg); 
             //Do the loop

but this is not doing it.... Scratching my head proper on this one and will appreciate some help/solution thx in advance

share|improve this question
    
any help please? –  salocin Mar 24 '11 at 10:20
add comment

1 Answer

You should query all accomodation posts in this country, and then sort them by accomodation-type and region. Then you loop through the posts, and compare the accomodation type and the region of each post with those of the previous post. If they are different you print the new accomodation type or region and then print the post.

// Pseudo-code to explain the idea
$prev_post_accomodation_type = $accomodations[0]->accomodation_type;
$prev_post_region = $accomodations[0]->region;
echo '<ul>';
echo '<li>' . $prev_post_accomodation_type . '<ul>';
echo '<li>' . $prev_post_region . '<ul>';
foreach ( $accomodations as $accomodation ) {
    $cur_post_accomodation_type = $accomodation->accomodation_type;
    $cur_post_region = $accomodation->region;
    if ( $prev_post_accomodation_type != $cur_post_accomodation_type ) {
        echo '</ul></li>'; // Close previous region list
        echo '</ul></li>'; // Close previous accomodation type list
        echo '<li>' . $cur_post_accomodation_type . '<ul>';
        echo '<li>' . $cur_post_region . '<ul>';
    } elseif ( $prev_post_region != $cur_post_region ) {
        echo '</ul></li>'; // Close previous region list
        echo '<li>' . $cur_post_region . '<ul>';
    }
    echo '<li>' . $accomodation->post_title . '</li>';
}
echo '</ul></li>'; // Close previous region list
echo '</ul></li>'; // Close previous accomodation type list
echo '</ul>';

You can do this sorting in the database or in PHP. In PHP could be easier, but will require many extra database queries because you do get_post_terms() for each post. In the database is faster but will require some filtering of the SQL query that WordPress will execute.

My spelling corrector wants to replace accomodation with accommodation, is it too late to change this in your code too?

share|improve this answer
    
Hi Jan, Thank you for trying but this got me more confused!. I have to create a new post with new code. I will appreciate some help –  salocin Mar 31 '11 at 8:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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