WP Get posts with any value in attribute - wordpress

Hello I am trying to do this query that extract woocommerce products with 3 attributes (Collezione, Finitura, Pietre). But we can have none,1,2 or 3 filters set, so I wanna ask you which value I have to set if the filter is not set. I thought -1 but this is not working here.
Here below you can find my code. Thanks in advance.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat_id,
'operator' => 'IN'
),
array(
'taxonomy' => 'pa_collezione',
'field' => 'term_taxonomy_id',
'terms' => $pa_collezione,
'operator' => 'IN'
),
array(
'taxonomy' => 'pa_finitura',
'field' => 'term_taxonomy_id',
'terms' => $pa_finitura,
'operator' => 'IN'
),
array(
'taxonomy' => 'pa_pietre',
'field' => 'term_taxonomy_id',
'terms' => $pa_pietre,
'operator' => 'IN'
)
)
);
$products = get_posts( $args );

I think your terms like 'pa_collezione' are your in your product_cat taxonomy.
Your tax_query should just have an array of terms for the terms field.
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $array_of_term_ids,
),
)
Your do your filter in your terms array, not by adding extra relations.

I've found a solution setting 'NOT IN' instead of 'IN' if parameter is not set.
Here below the solution.
$cat_id = $_POST['category'];
$pa_finitura= $_POST['finitura'];
$pa_collezione= $_POST['collection'];
$pa_pietre= $_POST['pietre'];
if($pa_collezione=="-1"){$cond_collezione='NOT IN';}else{$cond_collezione = 'IN';}
if($pa_finitura=="-1"){$cond_finitura='NOT IN';}else{$cond_finitura = 'IN';}
if($pa_pietre=="-1"){$cond_pietre='NOT IN';}else{$cond_pietre = 'IN';}
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'pa_collezione',
'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id'
'terms' => $pa_collezione,
'operator' => $cond_collezione // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'pa_finitura',
'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id'
'terms' => $pa_finitura,
'operator' => $cond_finitura // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'pa_pietre',
'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id'
'terms' => $pa_pietre,
'operator' => $cond_pietre // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = get_posts( $args );

Related

How to query Standard and Video Post Format in WordPress?

I am trying to query posts according to their post format. Currently, I have the video, gallery, and standard post formats on my site. It's quite easy to query these post formats individually. However, I have a combination of tax queries of 10 posts that are standard and video post format, my query fails.
Here's the code I have right now:
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'relation' => 'OR',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
I think, you should put the 'relation' => 'OR', into the 'tax_query'-Array.
You are adding 'relation' => 'OR' in the wrong place. it should be inside the tax_query array. check the below code.
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
USEFUL LINKS
taxonomy-parameters

How do I query posts that don't have both of two tags?

Say I have two tags, red and small. I want to get all posts that are not red and small. I want to include posts that are either red or small, but they can't be both.
The tag__not_in parameter won't work, because it'll exclude posts that are just red or just small.
I also tried the tax query below but then realized it's the same as the above.
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
'operator' => 'NOT IN',
),
)
I feel like there's got to be a way to do this and I'm just missing something simple. That and I'm getting confused by the logic inversions. Any ideas?
You can try again
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
),
)
);
$query_red_small = new WP_Query( $args );
$post_ids = wp_list_pluck( $query_red_small->posts, 'ID' );
$args = array(
'post_type' => 'post',
'post__not_in' => $post_ids
);
$query = new WP_Query( $args );
?>

Filter on Category and Postformat

I have just started in Wordpress Template development, I am getting used to it.
However I have been looking to find the solution here but unable to find the same question.
I would like to filter on Category and Post-Format, i have the following code the Filter on the Post Format works however not on the Category.
$videos = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-video'
),
'category_name=instructie-videos',
'posts_per_page=6',
'operator' => 'IN'
)
)
) );
$videos = new WP_Query( array(
'post_type' => 'your-post-type',
'posts_per_page' => 6,
'category_name' => 'instructie-videos',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' ),
'operator' => 'IN'
)
)
) );
maybe it's syntax mistake near category_name. Try above code
you should try as below:
$myposts = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN'
)
)
) );

Wordpress taxonomy query with both and , or condition

i have product post type and i have product_cat is the taxonomy . In product_cat i have red, hard, soft, pen entires .
So i have to get the products coming under red and it coming hard or soft
How can i get this ?
For a product that come under both red and hard and soft i can use the following query
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'red'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'hard'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'soft'
)
),
'post_type' => 'product',
'orderby' => 'title',
);
But what i need is red is must and either in soft or hard .
ie (red && (soft||hard ))
Please help .
you can try this way:
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'red' ),
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hard' ),
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'soft' ),
),
),
),
Is untested, but it should work!
If not, here some useful links:
https://codex.wordpress.org/Class_Reference/WP_Query
https://10up.com/blog/2013/wordpress-mixed-relationship-taxonomy-queries/

how do i combine post meta data and taxonomy in a WP_query $args array Worpresss

I'm trying to figure out how to get the following code to work as one wp_query . i'm trying to filter the loop on the meta values and the taxonomy values:
Both of these sections work independently but i'm looking to tie these into one query..?
I have tried various combinations but am getting no where....any ideas of the best approach to this issue...?
$args = array(
array(
'post_type' => 'job_listing',
'meta_key' => 'geo_short_address',
'meta_value' => $area
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_type',
'field' => 'term_id',
'terms' => $jobtype
),
array(
'taxonomy' => 'job_cat',
'field' => 'slug',
'terms' => $jobcat
)
)
);
It looks as if your argument array is one level too deep. Try this:
$args = array(
'post_type' => 'job_listing',
'meta_key' => 'geo_short_address',
'meta_value' => $area,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_type',
'field' => 'term_id',
'terms' => $jobtype
),
array(
'taxonomy' => 'job_cat',
'field' => 'slug',
'terms' => $jobcat
)
)
);
If that doesn't work, try this:
$args = array(
'post_type' => 'job_listing',
'meta_key' => 'geo_short_address',
'meta_query' => array(
array(
'key' => 'geo_short_address',
'value' => $area
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_type',
'field' => 'term_id',
'terms' => $jobtype
),
array(
'taxonomy' => 'job_cat',
'field' => 'slug',
'terms' => $jobcat
)
)
);

Resources