0

I have two CPT and want to order them with Events first, then Articles. Events should be ordered ASC by wpcf-date_time - the event's date, which is in the future. The Articles should be ordered by DESC by published date.

add_action( 'pre_get_posts', function ( $q )
{
    if ( !is_admin() // Only target the front end queries
         && $q->is_main_query() // Targets the main query only
         && $q->is_category() // Only target category pages
    ) {
        $today = date( 'Y-m-d' );
        $q->set( 'post_type', array( 'research_article', 'events' ) );
        $q->set( 'posts_per_page', 12 );
        $q->set( 'orderby', array(
            'meta_value' => 'ASC',
            'date' => 'DESC'
        ) );
        $q->set( 'meta_query', array(
            'relation' => 'OR',
            array( 
                'key' => 'wpcf-date_time',
                'value' => $today,
                'compare' => '>=',
                'type' => 'DATE',
                ),
            array(
                'key'      => 'wpcf-date_time',
                'compare'  => 'NOT EXISTS'
            ),
        ) );
    }
});

This results in Articles showing first, then Events. How can I make Events show first?

1
  • Did you tried simply change post types order in array you set to a query. $q->set( 'post_type', array( 'events','research_article' ) );
    – anton
    Apr 22 at 19:52

Your Answer

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

Browse other questions tagged or ask your own question.