0

Two post types blog and events. I want to list in the same query but order events by their event date and blogs by their published date:

                    $args = array(
                      'post_type' => array( 'post', 'event' ),
                      'posts_per_page' => '-1',
                    'meta_query' => array(
                        array(
                            'key' => 'event_start_date',
                            'compare' => '>'
                        )
                    ),
                    'orderby' => 'event_start_date post_date',
                    'order' => 'ASC'
                    );
                    query_posts($args);

The above doesn't display the blog posts.

0

It is unlikely that you will achieve this as stated. WordPress has no concept of post subsets in a query, it only understands it as single set as results.

If you don't need pagination you could just retrieve the set unsorted and re–sort it with PHP code. If you do need the pagination this would probably be extremely inconvenient and require massive custom SQL.

PS avoid query_posts(), it's outdated and harmful.

0

This is untested, but you can try an EXISTS OR NOT EXISTS meta_query:

$args = array(
    'post_type' => array( 'post', 'event' ),
    'posts_per_page' => '-1',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'event_start_date',
            'compare' => 'EXISTS'
        ),
        array(
            'key' => 'event_start_date',
            'compare' => 'NOT EXISTS'
        )
    ),
    'orderby' => array(
        'meta_value_num' => 'ASC',
        'post_date' => 'ASC'
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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