WordPress.org

Make WordPress Core

Opened 20 months ago

Closed 20 months ago

Last modified 19 months ago

#22437 closed defect (bug) (invalid)

Issue within WP_Meta_Query class

Reported by: nic0tin Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Users Keywords:
Focuses: Cc:

Description

When making a query using WP_User_Query class while filling 'meta_query' key with, for example, the following:

$options = array(
		'meta_query' => array(
		  array(
		    'meta_key' => 'custom_field_1',
		    'meta_value' => '1',
		    'meta_compare' => '!=',
		    'meta_type' => 'DECIMAL'
		  ),
		  array(
		    'meta_key' => 'custom_field_2',
		    'meta_value' => '',
		    'meta_compare' => '!='
		  )
	        )  
	);
$users = new WP_User_Query($options);

It doesn't consider the 'meta_query' array. I had a look into /wp-includes/meta.php and found out that when 'WP_User_Query.parse_query_vars' function is called, it parses the array naming keys with the 'meta_' prefix, then it calls 'WP_User_Query.construct' constructor which is setting '$this->queries[]' using the 'meta_' prefix as well.

The problem appears after, into 'WP_User_Query.get_sql' function, when the foreach loop is going through the array, it's trying to refer to array keys without the 'meta_' prefix.

Original:

$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';

...

if ( !isset( $q['value'] ) ) {

...

$meta_value = $q['value'];

...

if ( isset( $q['compare'] ) )
	$meta_compare = strtoupper( $q['compare'] );

Modified to:

$meta_key = isset( $q['meta_key'] ) ? trim( $q['meta_key'] ) : '';
$meta_type = isset( $q['meta_type'] ) ? strtoupper( $q['meta_type'] ) : 'CHAR';

...

if ( !isset( $q['meta_value'] ) ) {

...

$meta_value = $q['meta_value'];

...

if ( isset( $q['meta_compare'] ) )
	$meta_compare = strtoupper( $q['meta_compare'] );

By changing to the modified version, the query was generated with expected the INNER JOIN joints.

Change History (4)

comment:1 nic0tin20 months ago

  • Resolution set to invalid
  • Status changed from new to closed

Ok... Never mind. I had a look at the specifications of the WP_User_Query class providen by http://wpsmith.net/code/wp_user_query/

I ain't read the array keys correctly.

May be you should consider adding an example of 'meta_query' into the page: http://codex.wordpress.org/Class_Reference/WP_User_Query

Sorry about that.

comment:2 ocean9020 months ago

  • Keywords needs-patch removed
  • Milestone Awaiting Review deleted
  • Version 3.4.2 deleted

comment:3 SergeyBiryukov20 months ago

You can add a meta_query example to Codex yourself, just log in with the same username and password you did here.

comment:4 ramiy19 months ago

I have update the 'WP_User_Query' on the codex:

http://codex.wordpress.org/Class_Reference/WP_User_Query

Check it out!

Note: See TracTickets for help on using tickets.