0

I have a custom post type that im trying to limit results in search.

The CPT is a deal/coupon, with a start and end date for each of the deals.

What I'm trying to do is only show these posts in search:

  1. For non-recurring events....Show posts where the end date is today or after todays today.

OR

  1. For recurring events, limit it to show only events with an end date between today which does not exceed 7 days.

Each of these queries works perfectly on their own, but I cant combine them together...

function search_pre_get_posts( $query ) {

    if ( $query->is_search() && $query->is_main_query()   ) {  
        $recurring_count = am_get_recurring_count($post->ID);
        // Show non-recurring events with end date of today or after
        if ($recurring_count == 0){
            $currentdate =  date('Y-m-d');
            $query->set( 
                'meta_query',
                array(
                    'relation' => 'OR',
                    array(
                         'key' => 'am_enddate',
                         'compare' => '>=',
                         'value' => $currentdate,
                    ),
                    array(
                         'key' => 'am_enddate',
                         'compare' => 'NOT EXISTS',
                    )
                )
            );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
        } else {
           // Show recurring events with end date of today 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',
                         'compare' => 'NOT EXISTS',
                    ),
                    array(
                        'relation' => 'AND',
                        array(
                             'key' => 'am_enddate',
                             'compare' => '>=',
                             'value' => $currentdate,
                        ),
                        array(
                             'key' => 'am_enddate',
                             'compare' => '<=',
                             'value' => $enddate,
                        )
                    )
                )
            );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
        }
    }
}
add_action( 'pre_get_posts', 'search_pre_get_posts' ); 
  • I think you need to assign param type under you meta query, use DATE or DATETIME as value. – Jevuska Apr 3 '16 at 6:25
0

What about removing the if statement, and combining the queries into something like this:

'meta_query',
array(
'relation' => 'OR',
    array(
        array(
            'relation' => 'AND',
                array(
                    'key' => 'am_get_recurring_count',
                    'compare' => '=',
                    'value' => '0',
                ),
                array(
                    'relation' => 'OR',
                    array(
                         'key' => 'am_enddate',
                         'compare' => '>=',
                         'value' => $currentdate,
                    ),
                    array(
                         'key' => 'am_enddate',
                         'compare' => 'NOT EXISTS',
                        )
                )
        )
    )
    array(
        array(
            'relation' => 'AND',
                array(
                    'key' => 'am_get_recurring_count',
                    'compare' => '!=',
                    'value' => '0',
                ),
                array(
                    'relation' => 'OR',
                    array(
                         'key' => 'am_enddate',
                         'compare' => 'NOT EXISTS',
                    ),
                    array(
                        'relation' => 'AND',
                        array(
                             'key' => 'am_enddate',
                             'compare' => '>=',
                             'value' => $currentdate,
                        ),
                        array(
                             'key' => 'am_enddate',
                             'compare' => '<=',
                             'value' => $enddate,
                        )
                    )
                )
        )

   )

 )

Haven't had a chance to test this yet. Please let me know if it works.

| improve this answer | |
0

Try this

function search_pre_get_posts( $query ) {

    if ( $query->is_search() && $query->is_main_query()   ) {  
            $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',
                         'compare' => 'NOT EXISTS',
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'am_enddate',
                            'compare' => '>=',
                            'value' => $currentdate,
                        ),
                        array(
                            'key' => 'am_recurrence_id',
                            'compare' => 'NOT EXISTS',
                        )
                    ),
                    array(
                        'relation' => 'AND',
                        array(
                             'key' => 'am_enddate',
                             'compare' => '>=',
                             'value' => $currentdate,
                        ),
                        array(
                             'key' => 'am_enddate',
                             'compare' => '<=',
                             'value' => $enddate,
                        ),
                        array(
                            'key' => 'am_recurrence_id',
                            'compare' => 'EXISTS',
                        )
                    )
                )
            );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'search_pre_get_posts' ); 
| improve this answer | |

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.