Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_query and meta_query for repeater field values

I have a word press site with custom fields. I need to query a repeater field as a table. It looks like that:

        | param_1 | param_2 | param_2  | param_4
        |    20   |   20    | 20       |   20
        |    555  |   680   | 56       |    0
        |    5555 |   45    | 56       |    1
        |    69   |   0     | 45       |    0

I need to query this repeater to get a result only if it match the query in the same line

My meta query looks like that:

[post_type] => product
[posts_per_page] => -1
[orderby] => title
[order] => ASC
[meta_query] => Array (
    [relation] => AND
    [0] => Array (
        [key] => BBBproduct_params_values_AAA_param_value_0
        [value] => 25
        [compare] => <=
        [type] => NUMERIC
        )
    [1] => Array (
        [key] => BBBproduct_params_values_AAA_param_value_1
        [value] => 56
        [compare] => >=
        [type] => NUMERIC
        )
    [2] => Array (
        [key] => BBBproduct_params_values_AAA_param_value_2
        [value] => 56
        [compare] => >=
        [type] => NUMERIC
        )
    [3] => Array (
        [key] => BBBproduct_params_values_AAA_param_value_3
        [value] => 0
        [compare] => =
        [type] => NUMERIC
        )
    )

She is created trough this code:

if( $product_search_by_param_active ){
function my_posts_where( $where ){


    $where = str_replace("AAA_param_value_", "%_param_value_", $where);
    $where = str_replace("meta_key = 'BBBproduct_params_values_", "meta_key LIKE 'product_params_values_", $where);


    return $where;
}
add_filter('posts_where', 'my_posts_where');


$range_angine_settings = get_field('param_search_range_settings');
$search_args['meta_query'] = array();
$search_args['meta_query'] ['relation'] = 'AND';


foreach( $range_angine_settings as $rangekey => $search_range ){
    ${'range-'.$rangekey} = $_GET['range-'.$rangekey]? $_GET['range-'.$rangekey] : '0';
    if ( $_GET['range-'.$rangekey] ) $has_range_query = true; 
    //$pre_prama_value = 'product_params_values_'.strval($rangekey).'_param_value_AAA';

    if( isset($_GET['range-'.$rangekey])){
        $search_args['meta_query'][] = array(
                "key" => 'BBBproduct_params_values_AAA_param_value_'.strval($rangekey),
                "value"    => $search_range['search_metric_ranges'][${'range-'.$rangekey}]['range_value'],
                "compare"  => $search_range['product_search_logic'],
                'type'    => 'NUMERIC'
        );      
    }
}
}

The problem is that this because of the '%' I get result even if the query match not on the same line.

In the example above it is intended not to get any results, but as every condition is met on a different line I do get a result.

Is there a way to create this king of query to give results by line number?

like image 439
matisa Avatar asked Dec 25 '19 18:12

matisa


1 Answers

For me I prefer using CMB2 for custom fields https://github.com/CMB2/CMB2

like image 104
Mohamed Slimane Avatar answered Nov 06 '22 13:11

Mohamed Slimane