Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my query:

$the_query = new WP_Query(
    array (
        'post_type'         => 'pass',
        'posts_per_page'    => '-1',
        'post_status'       => 'publish',
        'orderby'           => 'meta_value',
        'meta_key'          => 'start_time',
        'order'             => 'ASC',
        'meta_query'        => array(
            'relation'      => 'OR',
            array(
                'key'       => 'event',
                'value'     => $tmp_posts->ID,
                'compare'   => '=='
            ),
            array(
                'key'       => 'meeting_type',
                'value'     => 'other',
                'compare'   => '!='
            )
        )
    )
);

My problem is this query does not order the posts by start_time. If I remove the second meta_query array (and the 'relation' => 'OR') then the orderby works great. Why is that?

If I var_dump $the_query this is the query outputted:

SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
WHERE 1=1 AND wp_posts.post_type = 'pass'
    AND (wp_posts.post_status = 'publish')
    AND (wp_postmeta.meta_key = 'start_time'
        OR (mt1.meta_key = 'event'
        AND CAST(mt1.meta_value AS CHAR) = '10011'
    )
    OR (
        mt2.meta_key = 'meeting_type'
        AND CAST(mt2.meta_value AS CHAR) != 'other')
    )
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value ASC

How can I get this to order my posts by start_time?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I was unable to create a WP_Query – I believe it is not possible to solve this using a regular WP_Query – but managed to create an SQL statement that does exactly what I want:

SELECT wp_posts.*
FROM wp_posts

INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)

WHERE 1=1 
    AND wp_posts.post_type = 'pass'
    AND (wp_posts.post_status = 'publish')
    AND (
        wp_postmeta.meta_key = 'start_time'
        AND (
            (
                mt1.meta_key = 'event'
                AND CAST(mt1.meta_value AS CHAR) = '10011'
            )
            OR (
                mt1.meta_key = 'meeting_type'
                AND CAST(mt1.meta_value AS CHAR) != 'other'
            )
        )
    )

GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value ASC
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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