0

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
5
  • Try specifying orderby too, e.g. '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.
    – Rup
    Commented Nov 22, 2020 at 9:50
  • Added UPDATE 01 above. Commented Nov 23, 2020 at 14:38
  • Still can't solve this one.... Commented Nov 25, 2020 at 14:04
  • Well you can see it's got "ORDER BY wp_y77uhd4169_posts.post_date ASC", so it's ordering on the wrong column, but I'm not sure why that would prevent you getting any results. Do you have a way to run this against your database to test this? Try removing bits to work out what's stopping you getting any results - maybe it's the order by, but I suspect it's the meta 'completed' = empty string? Can you see what query you get if you leave the order = ASC out?
    – Rup
    Commented Nov 25, 2020 at 19:15
  • The query runs perfect if I leave out the "order" entry. Granted it doesn't use the "orderby" but it at least returns the correct results just sorted by post date. As soon as I try the "order" field it returns empty no matter how many things I take out. It is really strange. Commented Nov 25, 2020 at 22:34

1 Answer 1

1

The root cause to my issue was using the custom post type of "order". Once I changed that to something more specific things worked fine. A complete guess but I'm wondering if the post type being "order" and the issue being the "order" query argument caused a conflict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.