I have multiple categories apllied to product, but woocommerce breadcrumbs choose only one line to show. I want to follow his line to show related products based on category shown in breadcrumbs.
Woocommerce breadcrumb.php file has
foreach ( $breadcrumb as $key => $crumb ) {
}
I want to know from where $breadcrumb goes to get same $crumb.
This solution for those who has product in several categories.
I found wc_get_product_terms(), this is how breadcrumbs created. So if you want to display related products based on category shown in breadcrumbs, you can do similar to this:
$term = wc_get_product_terms($product->get_id(), 'product_cat', array('orderby' => 'parent', 'order' => 'DESC'));
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $term[0]->term_id
),
)
);
$products = new WP_Query($args);
But I found a better way with yoast seo plugin:
$primary_term_product_id = yoast_get_primary_term_id('product_cat');
and then use $primary_term_product_id instead of $term[0]->term_id.
This solution for those who has product in several categories.
Related
Please don't judge this question harshly.
I have tried showing in Related Products only products from the same category in which the user is viewing the product right now, and by product tags (if applicable). But nothing has changed for me.
It turns out that WooCommerce already displays recommended products from the same category and by the same tags by default.
Below you will find the answer to this question. But let this code remain here simply as a sample code.
function related_products_by_current_category( $related_posts, $product, $args ) {
global $post;
$cat = $product->get_category_ids();
$tags = wp_get_post_terms( $post->ID, "product_tag" );
foreach ( $tags as $tag ) {
$tags_array[] .= $tag->term_id;
}
$related_posts = new WP_Query(
array(
'orderby' => 'rand',
'posts_per_page' => 4,
'post__not_in' => array($post->ID),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat
)
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
)
);
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'related_products_by_current_category', 999, 3 );
WooCommerce already fetches related products based on the same terms( categories and tags )
If you want to get more control over the related products by categories and tags, you can use these two filters
apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id );
apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id );
These filters return the current product categories | tags IDs which will be used to get related products. you can use these filters to return specific terms IDs based on the given product ID .
```$specialmenuItems = new WP_Query(array(
'post_type' => 'special_menu',
'posts_per_page' => -1,
));```
this is my custom post type (special menu), in the admin area there are three categories for this post type (dinner, drink and lunch). Main question is how can we display this post type at different locations based on different categories ?
You need to add the category (it is Taxonomy) to the WP_Query:
$specialmenuItems = new WP_Query( array(
'post_type' => 'special_menu',
'tax_query' => array(
array (
// Here is the taxonomy id
'taxonomy' => 'category',
'field' => 'slug',
// Here you put needed category slug
'terms' => 'dinner',
)
),
) );
while ( $specialmenuItems->have_posts() ) :
$specialmenuItems->the_post();
// Show Posts ...
endwhile;
wp_reset_postdata();
Is there a plugin or some code that after opening some category and all products are show to show products from other category. Because some category or sub-category have only 2-4 products and I want to fill the page with other category product similar.
Example: Gloves
Glove page title
2-4 products
And then some category Boots with 5-6 products
Thanks!
The simple way to do this may simply be to create multiple categories - eg an "apparel" category and assign it both gloves and boots. The more complicated way might be to use the WP_Query to generate a listing.
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'my-glove-category'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'my-boots-category'
)
)
);
$executedQuery = new WP_Query($args);
if ($executedQuery->have_posts()) {
while ($executedQuery->have_posts()) {
$executedQuery->the_post(); //increment the post pointer, getting us the next post in the list
echo '<h2>' . get_the_title() . '</h2>';
}
}
else {
echo 'No products were found.';
}
This example will grab every product that is either in my-glove-category or my-boots-category. If you want to sort by the category, that starts to become a bit tougher.
You can also use product_tag as a taxonomy for these queries, see the installed taxonomies and post types for WooCommerce.
The need is to show on the single product page sidebar the other products in the categories the product is included.
For example:
A bike is included in product categories (x,y,z)
When clicked at Bike, I am # the product single page.
In the sidebar right, i want to show all the products of X + Y + Z - Bike.
How can I reach this?
Current & desired situation
using Wordpress, WooCommerce, WidgetLogic and Divi template
You can try this code to achieve the results:
in the sidebar.php
if(is_product()) {
// if you are on single product page
$productObj =get_queried_object();
$categories = get_the_terms($productObj->id,'product_cat');
$cat_list = array();
foreach($categories as $category)
{$cat_list[] = $category->term_id;};
$posts = get_posts(
array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'no_found_rows' => true,
'exclude' => array($productObj->id),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $cat_list,
'field' => 'term_id'
)
)
)
);
// now you can iterate through the $posts and show the appropriate list.
}
I recently added post formats to my WordPress theme - on the blog page its fine as they are all styled accordingly. However on my home page template I only want to show 'standard' posts formats (no links, galleries, audio, video etc.).
In my theme options I can decide how many posts to display on the front page which is what 'dft_recent_number' is for.
Does anybody know how I can change the code below to exclude all but 'standard' post formats?
<?php
$query = new WP_Query();
$query->query('posts_per_page='.get_option('dft_recent_number'));
//Get the total amount of posts
$post_count = $query->post_count;
while ($query->have_posts()) : $query->the_post();
?>
Any help is much appreciated!
WP_Query does not seem to have a straightforward parameter for post_format.
From my quick research it appears that post formats are related through taxonomies. So, theoretically, using the taxonomy parameters should work.
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-standard',
)
)
);
$query = new WP_Query( $args );
Note: you'll need to update the taxonomy names and slugs for your blog. This should be the names you set in your functions.php file.
// there's no post-format-standard so you should write it like this to exclude all other postpformats
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
'operator' => 'NOT IN'
)
I know that's old, but I was facing the same issue and even though I've found a solution I wondered what other have done to "fix" that.
I think a more scalable solution could be something like that:
$post_formats = get_theme_support( 'post-formats' );
$tax_query = false;
if ( $post_formats ) {
$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_formats[0],
'operator' => 'NOT IN'
)
);
}
// WP_Query arguments
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => $tax_query
);
This would exclude the post formats that have been enabled and also will work in case WP will add more post formats (or add the ability to add more).