Leave a Reply

3 comments

  1. Did you try something like this:

    $loop = new WP_Query( array( 
        'post_type' => 'business_club',
        'meta_value' => 'United Kingdom',
        'order' => 'ASC',
        'orderby' => 'meta_value',
        'meta_key' => 'zone'
     ) ); 
    while ( $loop->have_posts() ) : $loop->the_post();
    

    I don’t know yet if you can do the then orderby town and title.

  2. If all results have the same zone, why would you want to order by zone?

    As far as I know, you can’t include two meta_keys in a single query. So you’ll have to write the query to return the results ordered by title, save them in an array, and then resort the array into the order you need using PHP.

  3. The way around it was to build a custom query. More information on this can be viewed on the WordPress codex at http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query.

    My final code looked as follows

    <?php               
    $row = 0;
    $zone = $_GET['zone'];
    if (!$zone) $zone = "United Kingdom";
    ?>
    
    <table class="formattedTable" cellspacing="1" cellpadding="0">
    <tr>
        <th width="140">Country</th>
        <th width="140">Town</th>
        <th>Club</th>
    </tr>
    <?php
    
    $query = "
    SELECT
      posts.*
    FROM
      $wpdb->posts posts
    INNER JOIN
      $wpdb->postmeta meta1 ON posts.ID = meta1.post_ID
    INNER JOIN
      $wpdb->postmeta meta2 ON posts.ID = meta2.post_ID
    INNER JOIN
      $wpdb->postmeta meta3 ON posts.ID = meta3.post_ID
    WHERE
      posts.post_type = 'club' AND
      posts.post_status = 'publish' AND
      meta1.meta_key = '_club-zone' AND
      meta1.meta_value = '$zone' AND
      meta2.meta_key = '_club-country' AND
      meta3.meta_key = '_club-town'
    ORDER BY
      meta2.meta_value,
      meta3.meta_value,
      posts.post_title";
    
    $posts = $wpdb->get_results($query, object);        
    if ($posts)
        foreach($posts as $post)
        {
            global $post;
            setup_postdata($post);
    
            echo '<tr class="' . ($row % 2 == 0 ? 'odd' : 'even') . '">';
            echo '<td>' . get_post_meta(get_the_ID(), '_club-country', true) . '</td>';
            echo '<td>' . get_post_meta(get_the_ID(), '_club-town', true) . '</td>';
            echo '<td><a href="' . get_permalink(get_the_ID()) . '">' . get_the_title() . '</a></td>';
            echo '</tr>';
            $row++;
        }
    
    ?>
    </table>