Support

Account

Home Forums General Issues Querying by relationship field Reply To: Querying by relationship field

  • @hube2 This actually worked for me! I was able to get all products that have relationship to certain cars:

    
    // Array of car IDs
    $cars = [1, 2, 3];
    $cars = array_map(function ($car_id) {
        return [
            'key' => 'cars',
            'value' => '"' . $car_id . '"',
            'compare' => 'LIKE',
        ];
    }, $cars);
    get_posts([
        'numberposts' => -1,
        'post_type' => 'product',
        'meta_query' => [
            'relation' => 'AND',
            [
                'relation' => 'OR',
                ...$cars,
            ],
        ]
    ]);
    

    Thank you!