I'm trying to do a WP_Query in a custom page template to retrieve posts that are in the category with id 4003 AND either in category with ID $category_id OR have the tag with slug $category_slug.
I'm using this code:
<?php $args3 = array(
'post_type' => 'post',
'posts_per_page' => 3,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4003,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'tag',
'field' => 'slug',
'terms' => $category_slug
),
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $category_id
)
)
),
);
$query3 = new WP_Query( $args3 );
?>
It seems that the problem is the tag query, because posts with both categories 4003 and $category_id are correctly retrieved. I know that there are posts with category 4003 and tag $category_slug but I cannot retrieve them.
What am I doing wrong?
Related
I try to get custom related posts for my woocommerce product page based on its attributes.
I set up the following php query, to find the products with the exact same attributes:
$pa_farbe = $attributes["pa_farbe"];
$pa_material = $attributes["pa_material"];
$pa_steine = $attributes["pa_steine"];
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $parent_cat,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
'value' => array($price_min, $price_max),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_farbe',
'field' => 'slug',
'terms' => $pa_farbe->get_slugs(),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_material',
'field' => 'slug',
'terms' => $pa_material->get_slugs(),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_steine',
'field' => 'slug',
'terms' => $pa_steine->get_slugs(),
'operator' => 'IN',
)
),
'exclude' => array( $product_id )
));
However, not all products have an attribute "pa_farbe" assigned. So for those products the php code shows an error on the frontend.
How can I check before, if the attribute is available for the current product?
I tried getting the attributes with this code:
foreach( $product->get_attributes() as $attr_name => $attr ){
echo wc_attribute_taxonomy_name( $attr_name );
}
but I don't know how to use an if query in the get_posts function.
I'm not an experienced developer so in my head this would be a solution that works but is not allowed in get_posts:
if (!sizeof($pa_farbe)=0) {
array(
'taxonomy' => 'pa_farbe',
'field' => 'slug',
'terms' => $pa_farbe->get_slugs(),
'operator' => 'IN',
),
}
Ideally the 'tax_query' => array() is filled with the current products attributes, could someone help me out here?
I have two categories called mobile and laptop, which I want to show both at the same time on the main page. I used this code but it only shows one of the categories. Thank you for your help
<?php $products= new WP_Query(
array(
'post_type'=>'product',
'posts_per_page'=>'-1',
'product_cat' => 'mobile+laptop',
'orderby' => 'rand',
)
); ?>
You can use tax_query. try the below code.
<?php
$products = new WP_Query(
array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'rand',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mobile'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'laptop'
)
),
)
);
?>
hello I want to show related products based on my custom query but I just want to show 'in_stocks' products and meta_query not working with tax_query. anyone can help me?
$query_args = array(
'posts_per_page' => 10,
'no_found_rows' => 1,
'post__not_in' => array( $product->get_id()),
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
) )
);
To remove out of stock products from your custom WP query, you need to add an additional array to tour tax query as follows:
$query_args = array(
'posts_per_page' => 10,
'no_found_rows' => 1,
'post__not_in' => array( $product->get_id() ),
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
'operator' => 'NOT IN'
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
),
);
Or also maybe using a meta query this way:
$query_args = array(
'posts_per_page' => 10,
'no_found_rows' => 1,
'post__not_in' => array( $product->get_id() ),
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
),
'meta_query' => array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
)
);
It should work.
Related: Show only WooCommerce in stock products with a WP_Query
i'm using Ajax and the following function (in functions.php) to pull in a product that has matching attributes. All works well but if there are no products that have an exact attribute (pa_three) then i want it to fetch a product with the next closest pa_three. pa_three is a length.
Any ideas on how i can achieve this?
<?php
function example_filter_function(){
$final_one = $_POST['final_one'];
$final_two = $_POST['final_two'];
$final_three = $_POST['final_three'];
$args = array(
'orderby' => 'date',
'post_type' => 'product',
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => 1,
),
array(
'taxonomy' => 'pa_one',
'field' => 'id',
'terms' => $final_one
),
array(
'taxonomy' => 'pa_two',
'field' => 'id',
'terms' => $final_two
),
array(
'taxonomy' => 'pa_three',
'field' => 'slug',
'terms' => $final_three
)
)
);
$query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()): $query->the_post();?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();
die();
}
add_action('wp_ajax_myfilter', 'example_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'example_filter_function');
?>
I am working with WP_Query to pull results from DB using multiple taxonomy from a custom post type.I have two taxonomy with drop down each.Taxonomy city and cuisine but if a dont't select any one of them results don't show.Actually i want to show results only with the key words even if these taxonomies are not selected.
My codes
$args = array(
'post_type' => 'listings',
's' => get_query_var( 's' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
),
),
);
$restaurant_query = new WP_Query( $args );
In what way does your query know when a taxonomy term is selected? You could try to add the tax_query with an if statement later on like this:
if ($term == 'cuisine') {
$args['meta_query'][] = array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
);
} elsif($term == 'city') {
$args['meta_query'][] = array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
);
}
This why the tax_query will only be added when the term is selected. I've had a few strange encounters with this issue in the past and this method fixed my issue quite a few times.