WC_Admin_List_Table_Orders::search_custom_fields()publicWC 1.0

Search custom fields as well as content.

Method of the class: WC_Admin_List_Table_Orders{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Admin_List_Table_Orders = new WC_Admin_List_Table_Orders();
$WC_Admin_List_Table_Orders->search_custom_fields( $wp );
$wp(WP_Query) (required)
Query object.

WC_Admin_List_Table_Orders::search_custom_fields() code WC 8.7.0

public function search_custom_fields( $wp ) {
	global $pagenow;

	if ( 'edit.php' !== $pagenow || 'shop_order' !== $wp->query_vars['post_type'] ) { // phpcs:ignore  WordPress.Security.NonceVerification.Recommended
		return;
	}

	$post_ids = isset( $_GET['s'] ) && ! empty( $wp->query_vars['s'] ) ? wc_order_search( wc_clean( wp_unslash( $_GET['s'] ) ) ) : array(); // phpcs:ignore  WordPress.Security.NonceVerification.Recommended

	if ( ! empty( $post_ids ) ) {
		// Remove "s" - we don't want to search order name.
		unset( $wp->query_vars['s'] );

		// so we know we're doing this.
		$wp->query_vars['shop_order_search'] = true;

		// Search by found posts.
		$wp->query_vars['post__in'] = array_merge( $post_ids, array( 0 ) );
	}

	if ( isset( $_GET['order_date_type'] ) && isset( $_GET['m'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$date_type  = wc_clean( wp_unslash( $_GET['order_date_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$date_query = wc_clean( wp_unslash( $_GET['m'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		// date_paid and date_completed are stored in postmeta, so we need to do a meta query.
		if ( 'date_paid' === $date_type || 'date_completed' === $date_type ) {
			$date_start = \DateTime::createFromFormat( 'Ymd H:i:s', "$date_query 00:00:00" );
			$date_end   = \DateTime::createFromFormat( 'Ymd H:i:s', "$date_query 23:59:59" );

			unset( $wp->query_vars['m'] );

			if ( $date_start && $date_end ) {
				$wp->query_vars['meta_key']     = "_$date_type"; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
				$wp->query_vars['meta_value']   = array( strval( $date_start->getTimestamp() ), strval( $date_end->getTimestamp() ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
				$wp->query_vars['meta_compare'] = 'BETWEEN';
			}
		}
	}
}