Example #1
0
/**
 * Customise the event post query during a search so that clients and employees are included in results.
 *
 * @since	1.0
 * @param	arr		$query		The WP_Query
 * @return	void
 */
function mdjm_event_post_search($query)
{
    global $pagenow;
    if (!is_admin() || 'mdjm-event' != $query->get('post_type') || !$query->is_search() || 'edit.php' != $pagenow) {
        return;
    }
    // If searching it's only useful if we include clients and employees
    $users = new WP_User_Query(array('search' => $_GET['s'], 'search_columns' => array('user_login', 'user_email', 'user_nicename', 'display_name')));
    // WP_User_Query
    $user_results = $users->get_results();
    // Loop through WP_User_Query search looking for events where user is client or employee
    if (!empty($user_results)) {
        foreach ($user_results as $user) {
            $results = get_posts(array('post_type' => 'mdjm-event', 'post_status' => 'any', 'posts_per_page' => -1, 'meta_query' => array('relation' => 'OR', array('key' => '_mdjm_event_dj', 'value' => $user->ID, 'type' => 'NUMERIC'), array('key' => '_mdjm_event_client', 'value' => $user->ID, 'type' => 'NUMERIC'), array('key' => '_mdjm_event_employees', 'value' => sprintf(':"%s";', $user->ID), 'compare' => 'LIKE'))));
            // get_posts
            if (!empty($results)) {
                foreach ($results as $result) {
                    $events[] = $result->ID;
                }
            }
        }
        // foreach( $users as $user )
        if (!empty($events)) {
            $query->set('post__in', $events);
            $query->set('post_status', array('mdjm-unattended', 'mdjm-enquiry', 'mdjm-contract', 'mdjm-approved', 'mdjm-failed', 'mdjm-rejected', 'mdjm-completed'));
        }
    }
    // if( !empty( $users ) )
}