How to add a search filter for shipments list?

search-filter

Copy and paste following codes on your child theme’s functions.php file.

//Add origin filter
add_action('restrict_manage_posts', 'wpcargo_custom_filter');
function wpcargo_custom_filter() {
	global $typenow, $wpcargo, $WPCCF_Fields;
	if ( $typenow == 'wpcargo_shipment' ) {
		//Change wpcargo_origin_field to the meta_key of the data you want to filter
		$get_wpcargo_origin = $WPCCF_Fields->get_field_options( 'wpcargo_origin_field' );
		if( !empty( $get_wpcargo_origin ) && is_array( $get_wpcargo_origin ) ) {
			echo '<select name="wpcargo_origin_field">';
				echo '<option value="">-- Select Origin --</option>';
				foreach( $get_wpcargo_origin as $origin ){
					$selected_val = isset($_REQUEST['wpcargo_origin_field']) && $_REQUEST['wpcargo_origin_field'] == $origin ? 'selected' : '';
					echo '<option value="'.$origin.'" '.$selected_val.'>'.$origin.'</option>';
				}
			echo '</select>';
		}
	};
}
add_filter('parse_query', 'wpc_custom_query');
function wpc_custom_query($query) {
	global $pagenow;
	$post_type = 'wpcargo_shipment';
	$q_vars    = &$query->query_vars;
	$get_wpc_query = array();
	$meta_query 	= array();
	if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($_GET['wpcargo_origin_field']) && $_GET['wpcargo_origin_field'] != '') {
		 $get_wpc_query[] = array(
			'key'     => 'wpcargo_origin_field',
			'value'   => $_GET['wpcargo_origin_field'],
			'compare' => '=',
		  );
	}
	//** Export custom query for WPCargo Shipment table
	$filter_metakey 	= apply_filters( 'wpcargo_shipment_query_filter', array( ) );
	if( !empty( $filter_metakey ) ){
		foreach ( $filter_metakey as $metakey ) {
			if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($_GET[$metakey]) && $_GET[$metakey] != '' ) {
				$compare = '=';
				if( is_array( $_GET[$metakey] ) ){
					$compare = 'IN';
				}
				$get_wpc_query[] = array(
					'key'     => $metakey,
					'value'   => $_GET[$metakey],
					'compare' => '=',
				);
			}
		}
	}
	$meta_query[] = array(
		$get_wpc_query
	);
	if(!isset($_GET['page']) && is_admin()) {
		$query->set( 'meta_query', $meta_query);
	}
}
0
    0
    Your Cart
    Your cart is emptyReturn to Shop