LLMS_Admin_Post_Table_Orders::modify_admin_search( obj $query )

Modify the search query for various post types before retrieving posts.


Description Description


Parameters Parameters

$query

(obj) (Required) WP_Query


Top ↑

Return Return

(obj)


Top ↑

Source Source

File: includes/admin/post-types/post-tables/class.llms.admin.post.table.orders.php

	public function modify_admin_search( $query ) {

		// on the admin posts order table
		// allow searching of custom fields
		if ( is_admin() && ! empty( $query->query_vars['s'] ) && isset( $query->query_vars['post_type'] ) && 'llms_order' === $query->query_vars['post_type'] ) {

			// What we are searching for
			$term = $query->query_vars['s'];

			// Search wp_users
			$user_query = new WP_User_Query( array(
				'search' => '*' . esc_attr( $term ) . '*',
				'search_columns' => array( 'user_login', 'user_url', 'user_email', 'user_nicename', 'display_name' ),
				'fields' => 'ID',
			) );

			// Search wp_usermeta for First and Last names
			$user_query2 = new WP_User_Query( array(
				'fields' => 'ID',
				'meta_query' => array(
					'relation' => 'OR',
					array(
						'key' => 'first_name',
						'value' => $term,
						'compare' => 'LIKE',
					),
					array(
						'key' => 'last_name',
						'value' => $term,
						'compare' => 'LIKE',
					),
				),
			) );

			$results = wp_parse_id_list( array_merge( (array) $user_query->get_results(), (array) $user_query2->get_results() ) );

			// add metaquery for the user id
			$meta_query = array(
				'relation' => 'OR',
				array(
					'key' => '_llms_user_id',
					'value' => $results,
					'compare' => 'IN',
				)
			);

			// we have to kill this value so that the query actually works
			$query->query_vars['s'] = '';

			// set the query
			$query->set( 'meta_query', $meta_query );

			// add a filter back in so we don't have 'Search results for ""' on the top of the screen
			// @note we're not super proud of this incredible piece of duct tape
			add_filter( 'get_search_query', function( $q ) {
				if ( '' === $q ) {
					return $_GET['s'];
				}
			} );

		} // End if().

		return $query;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.