Custom related posts with product attributes - php

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?

Related

WooCommerce: Show product bundles first in loop followed by best selling products

I want to show product bundles as first items of a custom WooCommerce product loop.
Unfortunately I'm not sure how to order by a product type.
I know that I could order by a meta_key for example.
At the moment I'm doing this already with this lines:
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
Do I need to add a second loop to do this?
Here's are my current arguments of the loop:
$args = array(
'posts_per_page' => 14,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'post__not_in' => array (get_the_ID()),
'post_type' => array('product'),
'tax_query' => array(
array(
'relation' => 'AND',
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'bundle',
'operator' => 'NOT IN',
),
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_stock_status',
'value' => 'instock'
),
),
);

How can i check if the category has products or empty wordpress?

hello guys i'm trying to know if a specific category with some custom attributes has a product or not but i don't know how to use this function to check if it has or no
$isset_products_in_category = new WP_Query(
array(
'post_type' => 'product',
'posts_per_page' => -1,
'relation'=>'AND',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '5',
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'operator' => 'IN',
'terms' => '2',
),
),
'ignore_stickie_posts' => true,
'fields' => 'ids',
)
);
'relation' => 'AND' should be part of the tax_query array in WP_Query() (it defines the relationship between the arrays in the tax_query).
Your not doing anything with the result of the query. You could check the count:
Revised code example:
<?php
$wp_query = new WP_Query(
array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'relation'=>'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '5',
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'terms' => '2',
'operator' => 'IN',
),
),
'ignore_sticky_posts' => true,
'fields' => 'ids',
)
);
// Do something with the result of the query
$isset_products_in_category = $wp_query->post_count > 0;
var_dump($isset_products_in_category); // will output bool(true) or bool(false).

Remove out of stock products from WooCommerce related products custom WP query

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

orderby and order filter in get_posts or WP_query function in wordpress not working

I have function in wordpress plugin which queries the posts using get_posts($array). But I wanted this to orderby post_modified field of the posts table in DESCENDING order, for which I have this code below:
$arrPostDtls = get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft') ,
'orderby' => 'post_modified',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Here, I did implemented the orderby or order clauses to get it sorted accordingly, but it doesnt work. Please suggest or help me to get the sorting as I am willing to.
UPDATE To get the things other way, I used the WP_query method to get the posts. for which I implemented below code:
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => 'modified',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
From this I recieved the result which also contains the SQL query, and executing the SQL query in PHPmyadmin, I found the exepected result, but when i iterated the "$arrPostDtls->posts", it still gives me the old results.. Please suggest what is wrong here..
Try
get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending',
'trash','draft','auto-draft') ,
'orderby' => array('post_modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Update after you update the question
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => array('modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
I found my problem on the UI side (thanks to #Zhilevan) where by ajax response was being exposed to a jQuery library DataTable(), whose default ordering was sorting in alphabetical order. I set the ordering parameter to false as:
$("#someid").DataTable({"ordering":false});
And My results were displayed as I was willing to

WordPress is it possible to have search by keyword of standard search query and custom tax_query

have query like this:
$key = 'xbox';
$test = get_posts(
array(
'suppress_filters' => false,
's' => $key,
'numberposts' => 10,
'post_type' => array('games'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'platforms',
'field' => 'slug',
'terms' => $key,
),
array(
'taxonomy' => 'genres',
'field' => 'slug',
'terms' => $key,
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $key,
)
)
)
);
and I need to have search s and tax-query working by both..
for example if I seach xbox I want that query would find and in my posts content/titlte word xbox.. but also check if any post exists in term LIKE %xbox%
any suggestions?

Categories