• Hi all

    just wanted to sort some meta values with meta_query. order by just one is easy. but order by two? (for ex. first order by ‘datum’ and then order by ‘zeit’)

    my code looks like this:

    $args = array(
    	'post_type' => 'events',
    	'posts_per_page' => 30,
    	'meta_query' => array(
    		array(
    			'key' => 'datum',
    		),
    		array(
    			'key' => 'zeit',
    		)
    	),
           'meta_key' => 'datum',
      	'orderby' => 'meta_value_num',
      	'order' => 'ASC'
     );
    
    $loop = new WP_Query( $args );

    could not find any solution for multiple ordering anywhere ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try the following:

    $args = array(
    	'post_type' => 'events',
    	'posts_per_page' => 30,
    	'meta_query' => array(
    	    array('key' => 'datum'),
                array('key' => 'zeit')
    	),
           'orderby'  => 'meta_value'
     );
    $loop = new WP_Query( $args );

    Thread Starter muelliman

    (@muelliman)

    thanks, but doesnt change anything ??

    You should try this by custom query as follows:

    <?php
    $query = "SELECT * FROM $wpdb->posts p
              LEFT JOIN $wpdb->postmeta pm1 ON p.ID = pm1.post_id
              LEFT JOIN $wpdb->postmeta pm2 ON p.ID = pm2.post_id
              WHERE
              p.post_type = 'events' AND p.post_status = publish' AND
              pm1.meta_key = 'datum' AND
              pm1.meta_value = 'yes' AND
              pm2.meta_key = 'zeit' AND
              pm2.meta_key = 'yes'
              ORDER BY
              pm1.meta_value,
              pm2.meta_value";
    
    $posts = $wpdb->get_results($query, object);
    ?>

    Please make sure your meta_value is yes or anything else.

    Thread Starter muelliman

    (@muelliman)

    thank you, I’ll try that later; will be away until monday.

    Thread Starter muelliman

    (@muelliman)

    hmmm… sound logical. BUT there is no result. Even if I try with meta vaules that actually ARE in the database:

    SELECT * FROM $wpdb->posts p
              LEFT JOIN $wpdb->postmeta pm1 ON p.ID = pm1.post_id
              LEFT JOIN $wpdb->postmeta pm2 ON p.ID = pm2.post_id
              WHERE
              p.post_type = 'events' AND p.post_status = publish' AND
              pm1.meta_key = 'datum' AND
              pm1.meta_value = '20120918' AND
              pm2.meta_key = 'zeit' AND
              pm2.meta_key = '22:00'
              ORDER BY
              pm1.meta_value,
              pm2.meta_value";

    if i delete all the meta arguments, there is a resultat.

    Change last ‘meta_key’ to meta_value’ as:

    pm2.meta_key = 'zeit' AND
    pm2.meta_value = '22:00'

    Thread Starter muelliman

    (@muelliman)

    oh, that was a big mistake ??

    but still no result…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘meta_query order by multiple keys’ is closed to new replies.