• I am trying to show some listings using wp_query and using meta_query.
    but I feel that sometimes the results are not accurate. I don’t know where’s the mistake

    
        $resultsargs = array(
        'post_type' => 'listings',
        'posts_per_page' => 3,
        'meta_query' => array( 
           'relation' => 'OR',
        'meta_query' => array(
            array(
                'key' => 'type_of_vehicle_c',
                'value' => $splittedNumbers,
                'compare' => '=',
            ),
            array(
                'relation' => 'AND', // only 'color' OR 'price' must match
                array(
                    'key' => 'interests_c',
                    'value' => $interestsNumbers,
                    'compare' => '=',
                ),
                array(
                    'key' => 'musthave_c',
                    'value' => $musthaveNumbers,
                    'compare' => '=',
                )
            )
        )
        )
        );

    NOTE: the keys ‘musthave_c’ and ‘interests_c’ are stored as serialized data in the database. and their values are returned as arrays in variables ‘$musthaveNumbers’ and ‘$interestsNumbers’

    after my selections it will show me the listings who have my selected values but it will also show me the listings who dont have my selected values
    like if the $interestsNumbers found and no values for $musthaveNumbers found then it will also show me some listings who just have $interestsNumber

    I just want to show listings that contain any of the values provided in the array but should have both $interestsNumbers and $musthaveNumbers found atleast 1 matching value.

    can anyone help?
    Thanks

    • This topic was modified 1 year, 11 months ago by Yui.
    • This topic was modified 1 year, 11 months ago by bcworkz. Reason: code format fixed
Viewing 1 replies (of 1 total)
  • You wont get any sensible response try to compare strings to serialized data with equals.

    You MIGHT get away with using LIKE %value% depending on what is stored but it is risky.

    The best way IMHO is to return all the relevant posts without the meta query

    and then loop through them getting the meta data, expanding the arrays and appropriately filtering.

    That might feel inefficient to do the filtering in PHP rather than SQL and a lot will depend on the number of posts returned in the initial query if it is a few hundred it will be irrelevant, if it is a few hundred thousand than perhaps some alternative plan is needed.

Viewing 1 replies (of 1 total)
  • The topic ‘how to get listings using meta query relation argument’ is closed to new replies.