0

I need to accomplish the following:

  • pull CPT tribe_events
  • sort by custom field "pin_to_top" in DESC order
  • get posts from today's date & any in the future
  • sort remaining posts in ASC order

My goal is to keep any posts with 'pin_to_top' = 1 at the top of the results, then display the remaining results in ASC order by event date.

Here's my query:

$data7 = array (
'post_status' => 'publish',
// create array of arrays
'meta_query' => array(
    // pin to top array
    array(
        'meta_key' => 'pin_to_top',
        'meta_type' => 'NUMERIC',
        'meta_value_num' => '1',
        'compare' => '='
        ),
    // event start date array
    array(
        'meta_key' => '_EventStartDate',
        'meta_type' => 'DATETIME',
        'meta_value' => '$today',
        'compare' => '>='
        ),
    ),
// custom post type
'post_type' =>
  array (
    'tribe_events' => 'tribe_events'
),
// categories of custom post type
'tribe_events_cat' => 'events',
// sorting
'orderby' => array(
    'pin_to_top' => 'DESC',
    '_EventStartDate' => 'ASC'
  ),
);

I am open to suggestions and please keep in mind that I'm just getting started with WP Dev. TIA

1
  • Why not using two queries? First for pin_to_top posts and then use the id's of those posts in second query with post__not_in parameter?
    – Abhik
    Commented Dec 15, 2016 at 16:15

1 Answer 1

0

I had a similar aproach some days ago

//.....
'meta_query'      => array(
           'relation' => 'OR',
            array(
                'pin_clause' = array(
                'meta_key' => 'pin_to_top',
                'meta_type' => 'NUMERIC',
                'meta_value_num' => '1',
                'compare' => '='
                )
            ),
            array(
                'date_clause' => array(
                    'meta_key' => '_EventStartDate',
                    'meta_type' => 'DATETIME',
                    'meta_value' => '$today',
                    'compare' => '>='
                )
            )
        ),
        'orderby' => array(
            'pin_clause' => 'DESC',
            'date_clause' => 'ASC'
        )
 //....

I don't know if I got your names right, but I think it will help to understand the trick.

2
  • I'm still unable to get this working and have setup a sandbox dev environment if anyone is willing to hep & teach me along the way. For money. Here's my updated query using your suggestions: codeshare.io/5gY3yG
    – dgfrench
    Commented Dec 16, 2016 at 17:00
  • Very important in the order part: // sorting 'orderby' => array( 'pin_clause' => 'DESC', 'date_clause' => 'ASC' ),
    – helle
    Commented Dec 19, 2016 at 9:51

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.