Support

Account

Home Forums General Issues Problem with Query on custom field

Solved

Problem with Query on custom field

  • I have a query

    add_filter( 'get_meta_sql', 'filter_meta_query' );
    
    		if (is_numeric($zip) && intval($zip) > 0) {
    			$EXTRA[] = array( 
    						'key' => 'mailing_zip', 
    						'value' => $zip, 
    						'compare' => 'LIKE' 
    					);
    					
    					
    			
    			$EXTRA['relation'] = 'OR';
    			$EXTRA[] = array( 
    						'key' => 'locator_address', 
    						'value' => $zip, 
    						'compare' => 'LIKE' 
    					);
    		
    		}
    
    $users = new WP_User_Query( 
    			array(
    				'meta_query' => 
    					$EXTRA
    			)
    		);
    

    When I print out this query for testing purposes, I notice that the ‘Like’ operator only shows as 32224% as opposed to %32224% – why is that? If I manually run the query in phpMyAdmin and add the leading ‘%’ the query runs as I want – from what I have read, the % are supposed to be added automatically, but it seems that the query adds the trailing % but not the leading.

    Any suggestions or what am I mssing?

    Here is the query that displays:

    SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.* FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id ) WHERE 1=1 AND (
    ( wp_usermeta.meta_key = ‘mailing_zip’ AND CAST(wp_usermeta.meta_value AS CHAR) LIKE ‘32224%’ )
    OR
    ( wp_usermeta.meta_key = ‘locator_address’ AND CAST(wp_usermeta.meta_value AS CHAR) LIKE ‘32224%’ )
    ) ORDER BY user_login ASC

  • Hi @dadofgage

    I’ve just tested your code like the following on my end, and it has the leading ‘%’ in the query.

    $zip = 123456;
    $EXTRA[] = array( 
        'key' => 'mailing_zip', 
        'value' => $zip, 
        'compare' => 'LIKE' 
    );
    $EXTRA['relation'] = 'OR';
    $EXTRA[] = array( 
        'key' => 'locator_address', 
        'value' => $zip, 
        'compare' => 'LIKE' 
    );
    
    $meta_query = new WP_Meta_Query();
    $meta_query->parse_query_vars( array(
    				'meta_query' => 
    					$EXTRA
    			) );
    $mq_sql = $meta_query->get_sql(
    	'post',
    	$wpdb->posts,
    	'ID',
    	null
    );
    
    print_r($mq_sql);

    Maybe there’s a custom function in your installation that modifies the query. Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Fifteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    Also, I believe this question is more related to WordPress than ACF. For further support, could you please ask it in the WordPress support forum?

    Thanks 🙂

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Problem with Query on custom field’ is closed to new replies.