WordPress.org

Ready to get started?Download WordPress

Forums

meta_query order by multiple keys (9 posts)

  1. muelliman
    Member
    Posted 1 year ago #

    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 :-(

  2. Chandan Kumar
    Member
    Posted 1 year ago #

    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 );
  3. muelliman
    Member
    Posted 1 year ago #

    thanks, but doesnt change anything :-(

  4. Chandan Kumar
    Member
    Posted 1 year ago #

    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.

  5. muelliman
    Member
    Posted 1 year ago #

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

  6. muelliman
    Member
    Posted 1 year ago #

    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.

  7. Chandan Kumar
    Member
    Posted 1 year ago #

    Change last 'meta_key' to meta_value' as:

    pm2.meta_key = 'zeit' AND
    pm2.meta_value = '22:00'
  8. muelliman
    Member
    Posted 1 year ago #

    oh, that was a big mistake :-(

    but still no result...

  9. Chandan Kumar
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic