Booking Filtering in Admin Page

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1765051

    Hi,
    I´d like to share a code snippet with you. I needed to filter reservations in the Admin panel by month. But when a month is selected only reservations that arrived in that month are shown not all reservations that have the check-in date in that particular month. So when a customer makes a reservation for lets say May in April it won´t show up in May but in April as it arrived in April.

    If you put this code snippet in your custom child funtions.php it should work. I recommend to make a backup as i don´t know the version you are working with or you set up a development server. I cannot give warranty or support 🙂

    // Add custom filters for stay year and month
    add_action(‘restrict_manage_posts’, ‘custom_mphb_booking_date_filters’);
    function custom_mphb_booking_date_filters($post_type) {
    if ($post_type !== ‘mphb_booking’) return;

    // Year Filter
    $current_year = date(‘Y’);
    $selected_year = isset($_GET[‘stay_year’]) ? sanitize_text_field($_GET[‘stay_year’]) : ”;
    echo ‘<select name=”stay_year”>’;
    echo ‘<option value=””>’ . esc_html__(‘Select year’, ‘your-textdomain’) . ‘</option>’;
    for ($year = $current_year – 2; $year <= $current_year + 2; $year++) {
    printf(
    ‘<option value=”%s” %s>%s</option>’,
    esc_attr($year),
    selected($selected_year, $year, false),
    esc_html($year)
    );
    }
    echo ‘</select>’;

    // Month Filter
    $selected_month = isset($_GET[‘stay_month’]) ? sanitize_text_field($_GET[‘stay_month’]) : ”;
    $months = array(
    ’01’ => __(‘January’, ‘your-textdomain’),
    ’02’ => __(‘February’, ‘your-textdomain’),
    ’03’ => __(‘March’, ‘your-textdomain’),
    ’04’ => __(‘April’, ‘your-textdomain’),
    ’05’ => __(‘May’, ‘your-textdomain’),
    ’06’ => __(‘June’, ‘your-textdomain’),
    ’07’ => __(‘July’, ‘your-textdomain’),
    ’08’ => __(‘August’, ‘your-textdomain’),
    ’09’ => __(‘September’, ‘your-textdomain’),
    ’10’ => __(‘October’, ‘your-textdomain’),
    ’11’ => __(‘November’, ‘your-textdomain’),
    ’12’ => __(‘December’, ‘your-textdomain’),
    );
    echo ‘<select name=”stay_month”>’;
    echo ‘<option value=””>’ . esc_html__(‘Stay month’, ‘your-textdomain’) . ‘</option>’;
    foreach ($months as $num => $name) {
    printf(
    ‘<option value=”%s” %s>%s</option>’,
    esc_attr($num),
    selected($selected_month, $num, false),
    esc_html($name)
    );
    }
    echo ‘</select>’;
    }

    // Modify query to filter by stay date
    add_filter(‘parse_query’, ‘custom_filter_mphb_bookings_by_stay_date’);
    function custom_filter_mphb_bookings_by_stay_date($query) {
    global $pagenow;

    // Nur im Admin-Bereich, nur auf der Buchungsliste, und nur auf der Hauptabfrage
    if (
    !is_admin() ||
    $pagenow !== ‘edit.php’ ||
    !isset($_GET[‘post_type’]) ||
    $_GET[‘post_type’] !== ‘mphb_booking’ ||
    !$query->is_main_query()
    ) {
    return;
    }

    $stay_month = isset($_GET[‘stay_month’]) ? sanitize_text_field($_GET[‘stay_month’]) : ”;
    $stay_year = isset($_GET[‘stay_year’]) ? sanitize_text_field($_GET[‘stay_year’]) : ”;

    if (empty($stay_month) || empty($stay_year)) {
    return;
    }

    $start_date = “{$stay_year}-{$stay_month}-01”;
    $end_date = date(‘Y-m-t’, strtotime($start_date));

    $meta_query = $query->get(‘meta_query’) ?: [];

    $meta_query[] = [
    ‘relation’ => ‘AND’,
    [
    ‘key’ => ‘mphb_check_in_date’,
    ‘value’ => $end_date,
    ‘compare’ => ‘<=’,
    ‘type’ => ‘DATE’,
    ],
    [
    ‘key’ => ‘mphb_check_out_date’,
    ‘value’ => $start_date,
    ‘compare’ => ‘>=’,
    ‘type’ => ‘DATE’,
    ]
    ];

    $query->set(‘meta_query’, $meta_query);
    }

    // Optional: Remove default “All dates” dropdown
    add_filter(‘months_dropdown_results’, ‘__return_empty_array’);

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.