How To Add Date Range Filter From Custom Field
1. On your WordPress Admin, go to WPCargo –> Manage Form Fields –> Add Form Field. After that, create a new field.
2. To add a custom date range filter on frontend dashboard, use this code:
function wpcfe_after_shipment_filters_callback(){ $default_date = date('Y-m-d'); ?> <div id="wpcfe-custom-shipping-date" class="form-group wpcfe-filter receiver-filter p-0 mx-1"> <div class="md-form form-group"> <?php _e('Shipping Date:', 'wpccustom-tu' ); ?> <input id="shipping_date_start" class="form-control daterange_picker start_date px-2 py-1 mx-2" style="width: 96px;" autocomplete="off" name="shipping_date_start" type="text" value="<?php echo isset($_GET['shipping_date_start']) ? $_GET['shipping_date_start'] : $default_date; ?>" placeholder="YYYY-MM-DD" /> <div class="input-group-addon"></div> <input id="shipping_date_end" class="form-control daterange_picker end_date px-2 py-1 mx-2" style="width: 96px;" autocomplete="off" name="shipping_date_end" type="text" value="<?php echo isset($_GET['shipping_date_end']) ? $_GET['shipping_date_end'] : $default_date; ?>" placeholder="YYYY-MM-DD" /></div> </div> <?php } add_action('wpcfe_after_shipment_filters', 'wpcfe_after_shipment_filters_callback');
3. In order for the custom date filter to work, you need to modify wpcargo’s default meta query. To do that, copy this code:
function custom_wpcfe_dashboard_meta_query_callback( $meta_query ){ $date_from = isset($_GET['shipping_date_start']) ? $_GET['shipping_date_start'] : ''; $date_to = isset($_GET['shipping_date_end']) ? $_GET['shipping_date_end'] : ''; if( !empty( $date_from ) && !empty( $date_to ) ){ $meta_query[] = array( 'shipping_date_clause' => array( 'key' => 'my_custom_date', 'value' => array($date_from, $date_to), 'compare' => 'BETWEEN', ), ); } return $meta_query; } add_filter( 'wpcfe_dashboard_meta_query', 'custom_wpcfe_dashboard_meta_query_callback' );