Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress - Order by multiple meta values after date, only showing dates beyond today
    text
    copied!<p>I have some custom fields named 'date' and 'start_time' and would like to order the posts by my custom field of 'date' and then the 'start_time'. I've got the following query - if anyone can point out where I'm going wrong that'd be great.</p> <p>The query I have is below:</p> <p>EDIT: The second array can be changed to anything - the field its self returns a value of anything between 0000 and 2359 - This is the value I need to sort by after the date </p> <p>So I have my query returning this at the moment</p> <p>Sun 26th May - Start Time: 0900 Sun 26th May - Start Time: 1300 Sun 26th May - Start Time: 2000 Sun 26th May - Start Time: 1030</p> <p>But I'd like this: </p> <p>Sun 26th May - Start Time: 0900 Sun 26th May - Start Time: 1030 Sun 26th May - Start Time: 1300 Sun 26th May - Start Time: 2000</p> <pre><code>$today = date("Ymd"); $args = array( 'post_type' =&gt; 'event', 'meta_query' =&gt; array( array( 'key' =&gt; 'date', 'value' =&gt; $today, 'type' =&gt; 'numeric', 'compare' =&gt; '&gt;' ), array( 'key' =&gt; 'start_time', 'value' =&gt; array( 0, 2359 ), 'compare' =&gt; 'BETWEEN' ) ), 'orderby' =&gt; 'meta_value', 'meta_key' =&gt; 'date', 'order' =&gt; 'ASC', 'paged'=&gt; $paged, 'posts_per_page'=&gt; 12 ); </code></pre> <p><strong>UPDATE</strong></p> <p>I can get it to work using an sql query : </p> <pre><code>&lt;?php $querystr = " SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta, $wpdb-&gt;postmeta wpostmeta2 WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND wpostmeta.meta_key = 'date' AND wpostmeta2.meta_key = 'start_time' AND wposts.post_type = 'event' AND wposts.post_status = 'publish' ORDER BY wpostmeta.meta_value ASC, wpostmeta2.meta_value ASC ";$pageposts = $wpdb-&gt;get_results($querystr, OBJECT); ?&gt; &lt;?php if ($pageposts): ?&gt; &lt;?php global $post; ?&gt; &lt;?php foreach ($pageposts as $post): ?&gt; &lt;?php setup_postdata($post); ?&gt; // STUFF &lt;?php endforeach; ?&gt; &lt;?php endif; ?&gt; </code></pre> <p>However, I can't get it to only return posts where the date is greater than today.</p> <p><strong>Update 2:</strong> </p> <p>Returning posts where date is greater than today was simple :</p> <pre><code>&lt;?php $today = date("Ymd"); $eventdate = get_field('date'); ?&gt; &lt;?php if($eventdate &gt; $today) : ?&gt; </code></pre>
 

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