• Resolved shuangmiles

    (@shuangmiles)


    Hi, I am working on a woocommerce site and I am writing a query with WP_Query I am looking to query products that either belong in this array of post id’s OR if as part of the same category as the product, and I’m not sure how to complete this tax_query. Any ideas?



    $args = array( 
        'post_type' => 'product', 
        'posts_per_page' => 4,
        // 'post__in' => $cross_upsells,
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy'      => 'product_cat',
                'field' => 'slug', //This is optional, as it defaults to 'term_id'
                'terms'         => $category_slug,
                'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
            ),
        ),
    );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    With WP_Query all the various args are ANDed. OR logic is only available within tax and meta queries themselves (and the fields searched with ‘s’). There’s no way to manage OR logic between the principal args like posts and taxonomies. You have 3 options:

    1. Make two different queries and merge the results in PHP.
    2. Through the “posts_request” filter, swap appropriate AND for OR in the SQL
    query.
    3. Compose your own SQL query and execute through the global $wpdb object.

    Thread Starter shuangmiles

    (@shuangmiles)

    Thanks I ended up doing the merge results and it seemed to work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘In WP_Query how to query ID’s as an OR with a category as the other criteria’ is closed to new replies.