carbon-fields
carbon-fields copied to clipboard
filter_get_meta_sql not filtering JOIN SQL
Version
- Carbon Fields: 2.2.0
- WordPress: 5.4.1
- PHP: 7.3
Actual Behavior
plugin not clear JOIN item in function filter_get_meta_sql
Container definition - SOLUTION
public function filter_get_meta_sql( $sql ) {
if ( strpos( $sql['where'], static::META_KEY_PREFIX ) !== false ) {
$sql['where'] = preg_replace( $this->get_meta_key_replace_regex(), '$1 LIKE $2', $sql['where'] );
}
if (isset($sql['join']) && strpos($sql['join'], static::META_KEY_PREFIX) !== false) {
$sql['join'] = str_replace(static::META_KEY_PREFIX, '', $sql['join'] );
}
return $sql;
}
Steps to Reproduce the Problem
call function like this:
add_action('pre_get_posts', 'wps_query');
function wps_query( $query ) {
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => '_is_last_ver', <<<<<<-------
'compare' => 'NOT EXISTS', <<<<<<-------
),
array(
'key' => '_is_last_ver',
'compare' => '=',
'value' => '1'
),
));
}
this fix helped me to make "NOT EXISTS" work as expected
This is still outstanding, so I added a filter to fix this issue:
function fix_carbon_fields_bug( $sql ) {
$prefix = 'carbon_fields:';
if ( isset( $sql['join'] ) && strpos( $sql['join'], $prefix ) !== false ) {
$sql['join'] = str_replace( $prefix, '', $sql['join'] );
}
return $sql;
}
add_filter( 'get_meta_sql', 'fix_carbon_fields_bug', 20, 1 );