carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

filter_get_meta_sql not filtering JOIN SQL

Open boomsya opened this issue 5 years ago • 2 comments

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'
				),
		    ));
}

boomsya avatar Jun 09 '20 11:06 boomsya

this fix helped me to make "NOT EXISTS" work as expected

brnteka avatar Sep 25 '20 23:09 brnteka

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 );

AdamWills avatar Sep 23 '21 13:09 AdamWills