生产服务器上复杂的WP_USER_QUERY调用失败

时间:2015-07-31 作者:hm711

在一个WP_User_Query 电话:

我正在使用以下(简化)代码构建查询:

// custom user role
$arguments = array(
    \'role\' => \'member\'
);

// number and offset
$arguments[\'number\'] = 10;
$arguments[\'offset\'] = 0;

$meta = array();
$meta[\'relation\'] = \'AND\';

// filter one (array)
$meta[] = array(
   \'key\'       => \'field-one\',
   \'value\'     => $values,
   \'compare\'   => \'IN\'
);

// filter two
$meta[] = array(
    \'key\'       => \'field-two\',
    \'value\'     => 0,
    \'compare\'   => \'=\'
);

// filter three
$meta[] = array(
    \'key\'       => \'field-three\',
    \'value\'     => 0,
    \'compare\'   => \'=\'
);

// even more filter
foreach( $filter as $key => $value ) {
    $meta[] = array(
        \'key\'     => \'field-\' . $key,
        \'value\'   => $value,
        \'compare\' => \'LIKE\'
    );
}

$arguments[\'meta_query\'] = $meta;

// orderby
$arguments[\'meta_key\'] = \'last_name\';       
$arguments[\'orderby\']  = \'meta_value\'; 

$query = new WP_User_Query(
    $arguments
);
在我的localhost(MySQL版本5.6.23)上,它正确地返回结果和正确的$query->total_users. 在生产站点(MySQL版本5.5.42-37.1-log)$query->get_results() 为空,但$query->total_users 显示任意数字。

当我打印WP_User_Query 对象我可以检查生成的SQL查询,该查询在本地主机和生产服务器上都是相同的:

DISTINCT SQL_CALC_FOUND_ROWS wp_users.* [query_from] => FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id ) INNER JOIN wp_usermeta AS mt1 ON ( wp_users.ID = mt1.user_id ) INNER JOIN wp_usermeta AS mt2 ON ( wp_users.ID = mt2.user_id ) INNER JOIN wp_usermeta AS mt3 ON ( wp_users.ID = mt3.user_id ) INNER JOIN wp_usermeta AS mt4 ON ( wp_users.ID = mt4.user_id ) INNER JOIN wp_usermeta AS mt5 ON ( wp_users.ID = mt5.user_id ) [query_where] => WHERE 1=1 AND ( ( ( wp_usermeta.meta_key = \'last_name\' AND ( ( mt1.meta_key = \'field-one\' AND CAST(mt1.meta_value AS CHAR) IN (\'professional\',\'member\',\'junior\') ) AND ( mt2.meta_key = \'field-two\' AND CAST(mt2.meta_value AS CHAR) = \'0\' ) AND ( mt3.meta_key = \'field-three\' AND CAST(mt3.meta_value AS CHAR) = \'0\' ) AND ( mt4.meta_key = \'field-four\' AND CAST(mt4.meta_value AS CHAR) LIKE \'%my-value%\' ) ) ) AND ( mt5.meta_key = \'wp_capabilities\' AND CAST(mt5.meta_value AS CHAR) LIKE \'%\\"member\\"%\' ) ) ) [query_orderby] => ORDER BY wp_usermeta.meta_value ASC [query_limit] => LIMIT 20 )
你知道这有什么问题吗?

1 个回复
最合适的回答,由SO网友:hm711 整理而成

在MySQL中执行生成的SQL语句时

The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
为了解决这个问题,我不得不打电话$wpdb->query( \'SET OPTION SQL_BIG_SELECTS = 1\' ); 就在打电话之前WP_User_Query.

结束

相关推荐