Site icon Hip-Hop Website Design and Development

Conditional posts in WP_query for search

I’ve a customized submit kind that im attempting to restrict ends in search.

The CPT is a deal/coupon, with a begin and finish date for every of the offers.

What I am attempting to do is simply present these posts in search:

  1. For non-recurring occasions….Present posts the place the top date is at the moment or after todays at the moment.

OR

  1. For recurring occasions, restrict it to indicate solely occasions with an finish date between at the moment which doesn’t exceed 7 days.

Every of those queries works completely on their very own, however I cant mix them collectively…

perform search_pre_get_posts( $question ) {

    if ( $query->is_search() && $query->is_main_query()   ) {  
        $recurring_count = am_get_recurring_count($post->ID);
        // Present non-recurring occasions with finish date of at the moment or after
        if ($recurring_count == 0){
            $currentdate =  date('Y-m-d');
            $query->set( 
                'meta_query',
                array(
                    'relation' => 'OR',
                    array(
                         'key' => 'am_enddate',
                         'evaluate' => '>=',
                         'worth' => $currentdate,
                    ),
                    array(
                         'key' => 'am_enddate',
                         'evaluate' => 'NOT EXISTS',
                    )
                )
            );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
        } else {
           // Present recurring occasions with finish date of at the moment or after, that doesnt exceed 7 days.
            $currentdate =  date('Y-m-d');
            $enddate = strtotime(date('Y-m-d') . "+7 days");
            $enddate = date('Y-m-d',$enddate);
            $query->set( 
                'meta_query',
                array(
                    'relation' => 'OR',
                    array(
                         'key' => 'am_enddate',
                         'evaluate' => 'NOT EXISTS',
                    ),
                    array(
                        'relation' => 'AND',
                        array(
                             'key' => 'am_enddate',
                             'evaluate' => '>=',
                             'worth' => $currentdate,
                        ),
                        array(
                             'key' => 'am_enddate',
                             'evaluate' => '<=',
                             'worth' => $enddate,
                        )
                    )
                )
            );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
        }
    }
}
add_action( 'pre_get_posts', 'search_pre_get_posts' );