logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Order By Multiple Meta Fields

I currently have the following:

new WP_Query('post_type=listings&meta_key=Asking Price&orderby=meta_value_num&order=DESC')


I would like to order the values by the meta key "Status", then "Asking Price". How do I do both?

This question has been answered.

Mark Daoust | 03/25/11 at 1:50pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    03/25/11
    2:20pm
    Sébastien | French WordpressDesigner says:

    what the possible value for "Asking Price" and for "Status" ?

    • 03/25/11 2:31pm

      Mark Daoust says:

      Asking price will be values such as $100,000, $35,000, or similar
      Status will be 'offer', 'sold', 'available'

    • 03/25/11 2:37pm

      Sébastien | French WordpressDesigner says:


      <?php
      $my_query = new WP_Query('post_type=listings&meta_key=Asking Price&orderby=meta_value_num&order=DESC');

      while ($my_query->have_posts()) : $my_query->the_post();
      $status = get_post_meta($post->ID, 'Status', true);
      if ($status="offer"){the_title();}
      endwhile;

      while ($my_query->have_posts()) : $my_query->the_post();
      $status = get_post_meta($post->ID, 'Status', true);
      if ($status="sold"){the_title();}
      endwhile;

      while ($my_query->have_posts()) : $my_query->the_post();
      $status = get_post_meta($post->ID, 'Status', true);
      if ($status="avalaible"){the_title();}
      endwhile;

      ?>

    • 03/25/11 3:29pm

      Sébastien | French WordpressDesigner says:

      i think there is a problem with orderby=meta_value_num
      that's strange...

      try to create 3 post with this value for Asking price : 8, 9 and 10

      try with the code of yves vu. I think that the ordre will be 10, 8, 9
      could you test ?

      It seems that the order is 10,2,3,4,5,6,7,8,9

      @Yves vu : do you look at this "bug" ?

    • 03/25/11 3:35pm

      Sébastien | French WordpressDesigner says:

      arf, what i said ?^^
      my precedent post is a big mistake
      sorry, i'm tired, i go to my bed !

    • 03/25/11 6:21pm

      Sébastien | French WordpressDesigner says:

      @yves vu > after testing, it seems that your solution doesn't work. We can't use two times the same argument (meta_key, orderby, order)

  • avatar
    Last edited:
    03/25/11
    2:48pm
    yves vu says:

    Hi,
    Please use this code:
    new WP_Query('post_type=listings&meta_key=Status&orderby=meta_value&order=DESC&meta_key=Asking Price&orderby=meta_value_num&order=DESC').

    You can change value "DESC" for testing.

    Tnks for reading
    Phucvh

  • avatar
    Last edited:
    03/31/11
    10:46am
    AdamGold says:

    http://www.mattvarone.com/wordpress/query-multiple-meta-values/

    • 03/25/11 6:30pm

      AdamGold says:

      Or you can use this code:


      $querydetails = "
      SELECT wposts.*
      FROM $wpdb->posts wposts
      INNER
      JOIN ( SELECT post_id
      FROM $wpdb->postmeta wpostmeta
      WHERE ( ( wpostmeta.meta_key = 'Status'
      AND wpostmeta.meta_value = '$status' )
      OR ( wpostmeta.meta_key = 'price'
      AND wpostmeta.meta_value '$price' )
      )
      GROUP
      BY post_id
      HAVING COUNT(*) = 4 ) AS t
      ON t.post_id = wposts.ID
      WHERE wposts.post_status = 'publish'
      AND wposts.post_type = 'listings'
      ORDER
      BY wposts.post_date DESC
      ";

      $pageposts = $wpdb->get_results($querydetails, OBJECT);

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.