Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe this will provide you want you need. It's a class called <code>PostsOrderedByMetaQuery</code> that extends <code>WP_Query</code> and accepts new arguments <code>'orderby_meta_key'</code> and '<code>orderby_order'</code>:</p> <pre><code>class PostsOrderedByMetaQuery extends WP_Query { var $posts_ordered_by_meta = true; var $orderby_order = 'ASC'; var $orderby_meta_key; function __construct($args=array()) { add_filter('posts_join',array(&amp;$this,'posts_join'),10,2); add_filter('posts_orderby',array(&amp;$this,'posts_orderby'),10,2); $this-&gt;posts_ordered_by_meta = true; $this-&gt;orderby_meta_key = $args['orderby_meta_key']; unset($args['orderby_meta_key']); if (!empty($args['orderby_order'])) { $this-&gt;orderby_order = $args['orderby_order']; unset($args['orderby_order']); } parent::query($args); } function posts_join($join,$query) { if (isset($query-&gt;posts_ordered_by_meta)) { global $wpdb; $join .=&lt;&lt;&lt;SQL INNER JOIN {$wpdb-&gt;postmeta} postmeta_price ON postmeta_price.post_id={$wpdb-&gt;posts}.ID AND postmeta_price.meta_key='{$this-&gt;orderby_meta_key}' SQL; } return $join; } function posts_orderby($orderby,$query) { if (isset($query-&gt;posts_ordered_by_meta)) { global $wpdb; $orderby = "postmeta_price.meta_value {$this-&gt;orderby_order}"; } return $orderby; } } </code></pre> <p>You would call it like this:</p> <pre><code>$thirtydays = date('Y/m/d', strtotime('+30 days')); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query = new PostsOrderedByMetaQuery(array( 'post_type' =&gt; array('post', 'real-estate'), 'meta_key' =&gt; 'Time Available', 'meta_compare' =&gt; '&lt;=', 'meta_value' =&gt; $thirtydays, 'paged' =&gt; $paged, 'orderby_meta_key' =&gt; 'Price', 'orderby_order' =&gt; 'DESC', )); foreach($query-&gt;posts as $post) { echo " {$post-&gt;post_title}\n"; } </code></pre> <p>You can copy the <code>PostsOrderedByMetaQuery</code> class to your theme's <code>functions.php</code> file, or you can use it within a <code>.php</code> file of a plugin you may be writing. </p> <p>If you want to test it quickly I've posted <a href="https://gist.github.com/665303" rel="nofollow noreferrer"><strong>a self-contained version of the code</strong></a> to Gist which you can download and copy to your web server's root as <code>test.php</code>, modify for your use case, and then request from your browser using a URL like <code>http://example.com/test.php</code>.</p> <p>Hope this helps.</p> <p>-Mike</p> <p>P.S. This answer is <a href="https://wordpress.stackexchange.com/questions/3708/custom-taxonomy-wp-query-for-all-terms-in-a-taxonomy/3715#3715"><strong>very similar to an answer I just gave over at WordPress Answers</strong></a>, which is the sister site of StackOverflow where lots of WordPress enthusiasts like me answer questions daily. You might want to <a href="https://wordpress.stackexchange.com/questions/3708/custom-taxonomy-wp-query-for-all-terms-in-a-taxonomy/3715#3715">see that answer too</a> because it has a tad more explanation and because you might want to see <a href="https://wordpress.stackexchange.com/">WordPress Answers</a>. Hope you'll consider posting your WordPress questions over <a href="https://wordpress.stackexchange.com/">there</a> too in the future?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload