WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank

If you haven’t done so already, I would recommend you to turn on debug logging in wp-config.php as it makes it easier to trace the error in your code. // added to wp-config.php define( ‘WP_DEBUG’, true ); define( ‘WP_DEBUG_LOG’, true ); // find error.log in /wp-content, shows backtrace for errors define( ‘WP_DEBUG_DISPLAY’, false ); // … Read more

How to add tax_query to $args with concatenation

Try this… <?php if ( $value[ ‘priceRange’ ] == 0 && $value[ ‘tags’ ] == 0 && !$value[ ‘type’ ] ) { if ( !$value[ ‘priceRange’ ] ) { $value[ ‘priceRange’ ] = array( 0, 1000000 ); } $args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => -1, ); } if ( $value[ ‘priceRange’ ] == … Read more

exclude product with available tag

You can setup a tax_query with the NOT IN operator. WP_Query( array( ‘post_type’ => ‘product’, ‘posts_per_page’ => 500, ‘tax_query’ => array( ‘relation’ => ‘AND’ array( ‘taxonomy’ => ‘product_cat’, ‘field’ => ‘slug’, ‘terms’ => array( ‘cars’ ), ), array( ‘taxonomy’ => ‘product_tag’, ‘field’ => ‘slug’, ‘terms’ => array( ‘whatever_tag_i_dont_need’ ), ‘operator’ => ‘NOT IN’, ), ), … Read more

How to exclude a taxonomy from shop & search page wooCommerce?

This might help you with Shop page listing: https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/ And for Search page, here is a snippet that might help: function exclude_services_category_products_from_search( $query ) { if ( is_search() && !is_admin() ) { $array_with_service_category_id = []; $tax_query = $query->get( ‘tax_query’ ) ?: []; $tax_query[] = [ ‘taxonomy’ => ‘product_cat’, ‘terms’ => $array_with_service_category_id, ‘field’ => ‘term_id’, ‘operator’ … Read more

Very complex post query

I got inspired in solving my problem by this thread! The idea is to create individual queries (unordered to save time, as sorting is not needed at this stage) to get the post IDs that meet your criteria; then take those IDs and run a final query with the IDs as the criteria to get … Read more

Tax query with multiple terms in pre_get_posts

Just use: ‘terms’ => $rt_cat_id I’ll work for both array and single-based values. Or you can simplify your code as follows: if( isset( $_GET[ ‘listing_cat’ ] ) ) { $tax_query[] = array( ‘taxonomy’ => ‘listing_category’, ‘field’ => ‘id’, ‘terms’ => $_GET[ ‘listing_cat’ ] ); }

Tax Query only returns for the first of several terms

Related issue: Multiple relationship for multiple tax_query in WP_Query The issue here was that mixed multi-relationship lookups are only permitted using the term_taxonomy_ids field in the query, the above worked fine once switched like so: $params = [ “status” => “publish”, “tax_query” => [ [ “taxonomy” => “jewellery_metal”, “field” => “term_taxonomy_id”, “terms” => [ 36, … Read more