Have never encountered anything like this. Running a simple query of custom post types, show only those that the current user is the author, and sort them by a custom meta field.
global $current_user;
$user = wp_get_current_user();
$args = array(
'post_type' => 'order',
'post_status' => 'publish',
'author' => $current_user->ID,
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => 'completed',
'value' => ''
)
),
);
$query = new WP_Query( $args );
This returns results. When I add in
'order' => 'ASC',
The Query returns no results. Makes absolutely no sense to me.
Just for reference, here is the entire section.
// ========== ORDERS QUERY CODE ==========
$orders = '';
global $current_user;
$user = wp_get_current_user();
$args = array(
'post_type' => 'order',
'post_status' => 'publish',
'author' => $current_user->ID,
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => 'completed',
'value' => ''
)
),
'meta_key' => 'delivery_date',
'order' => 'ASC',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$thePostID = get_the_ID();
$shortDesc = wordwrap(substr(get_the_content(), 0, 100), 99, '...');
$quantity = get_post_meta( $thePostID, 'order_quantity', true );
$orders .= '<div class="order-single order-'.$thePostID.'">
<div class="row">
<div class="col-4">
<div class="single-cal">
<div class="month">'.date_i18n('F', strtotime(get_post_meta( $thePostID, 'delivery_date', true ))).'</div>
<div class="day">'.date_i18n('j', strtotime(get_post_meta( $thePostID, 'delivery_date', true ))).'</div>
</div>
</div>
<div class="col-8">
<a class="order-customer" href="'.get_the_permalink().'">
'.get_the_title().'
</a>
<div class="order-desc">
<div class="qty"><strong>Quantity:</strong> '.$quantity.'</div>
'.$shortDesc.'
</div>
</div>
</div>
</div>';
}
} else {
$orders .= 'No Orders Found. <a href="/add">Click Here</a> to enter your first Order!';
}
wp_reset_postdata();
UPDATE 01: Installed plugin. Not sure how to use it but it looks like here is the SQL for that call.
SELECT wp_y77uhd4169_posts.*
FROM wp_y77uhd4169_posts
INNER JOIN wp_y77uhd4169_postmeta
ON ( wp_y77uhd4169_posts.ID = wp_y77uhd4169_postmeta.post_id )
INNER JOIN wp_y77uhd4169_postmeta AS mt1
ON ( wp_y77uhd4169_posts.ID = mt1.post_id )
WHERE 1=1
AND wp_y77uhd4169_posts.post_name = 'asc'
AND wp_y77uhd4169_posts.post_author IN (1)
AND ( wp_y77uhd4169_postmeta.meta_key = 'delivery_date'
AND ( ( mt1.meta_key = 'completed'
AND mt1.meta_value = '' ) ) )
AND wp_y77uhd4169_posts.post_type = 'order'
AND ((wp_y77uhd4169_posts.post_status = 'publish'))
GROUP BY wp_y77uhd4169_posts.ID
ORDER BY wp_y77uhd4169_posts.post_date ASC
'orderby' => 'meta_value'
. But that seems wrong to me that it would just fail - I'd expect it to print a warning and return unordered results if that's the problem. If that still doesn't work, please try and find the SQL that WordPress is actually running for this, e.g. using the Query Monitor plugin, so we can see what's wrong with it.