Im using this php code on my page to show subcategories, and when click on certain subcategory should redirect to posts that have that subcategory:
<?php
wp_list_categories( array(
'taxonomy' => 'categorys',
'title_li' => '',
'child_of' => get_term_by( 'slug', 'osten', 'categorys' )->term_id,
) );
?>
I created taxonomy.php
I have 6 categories to filter these posts and each Category have its subcategories.
SO i need to filter posts, when i click on subcategory to show me only posts on it.
I used this code inside taxonomy.php but its showing me all of posts and not filtering by clicked subcategory:
<?php
$post_type = 'ortemp';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies($post_type); // names (default)
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
echo '<div id="activities_categories">';
foreach( $terms as $term ) :
$customposts = new WP_Query(
array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $term->slug,
'field' => 'slug',
// 'category' => 'category-1'
)
)
)
);
if( $customposts->have_posts() ):
while( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
endforeach;
echo '</div>';
endforeach;
?>
try these code to your 'taxonomy.php' file.
$currentTerm = get_queried_object();
$subTerms = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );
if ( ! empty( $subTerms ) ) {
echo '<div id="activities_categories">';
foreach ( $subTerms as $subTerm ) :
$customposts = new WP_Query(
array(
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $currentTerm->taxonomy,
'terms' => $subTerm,
'field' => 'term_id',
)
)
)
);
if ( $customposts->have_posts() ):
while ( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
endforeach;
echo '</div>';
}
another example
$currentTerm = get_queried_object();
$subTerms = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );
if ( ! empty( $subTerms ) ) {
echo '<div id="activities_categories">';
$customposts = new WP_Query(
array(
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $currentTerm->taxonomy,
'field' => 'term_id',
'terms' => $subTerms,
)
)
)
);
if ( $customposts->have_posts() ):
while ( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
echo '</div>';
}
Related
So I have a custom type called ("knowledge_base") which has a taxonomy called ('section') and one of them is ('dog bite'). Right now I am on example.com/section/dog-bite/ and I am trying to show the posts located here. This is what I have so far so I am not sure what is missing but it just displays ALL the posts from all sections.
$current = get_queried_object();
$args = array(
'post_type' => 'knowledge_base',
'tax_query' => array(
array(
'taxonomy' => 'section',
'field' => $current->slug,
'terms' => $current->name
)
)
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
There is supposed to be only 2 posts
Check this code.
$args = array(
'post_type' => 'knowledge_base',
'tax_query' => array(
array(
'taxonomy' => 'section',
'field' => 'slug', // ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’
'terms' => $current->slug, // It's will be $term->slug
)
)
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
I have a custom archive-product template for displaying my product categories with headers in the shop front page. It also allows me to hide certain categories from the shop page. These edits were applied to the newest version of the archive-product.php. The problem is that when I go to the specific category page, no products are shown, it is just blank. If I revert the archive-product template back to default, the product category pages are then populated. Below is the php added to the template, is there something that seems to be wrong with it?
Thanks!
<?php
if ( woocommerce_product_loop() ) {
/**
* Hook: woocommerce_before_shop_loop.
*
* #hooked woocommerce_output_all_notices - 10
* #hooked woocommerce_result_count - 20
* #hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
/* Category - SubCategory START */
$term = get_queried_object();
$parent_id = empty( $term->term_id ) ? 0 : $term->term_id;
$excludedCats = array(47);
$product_categories = get_categories( array( 'taxonomy' => 'product_cat', 'child_of' => $parent_id, 'exclude' => $excludedCats) );
$i = 1;
foreach ($product_categories as $product_category) {
echo '<h2>'.$product_category->name.'</h2>';
woocommerce_product_loop_start(); //open ul
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
),
),
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'asc',
);
$cat_query = new WP_Query( $args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; // end of the loop.
wp_reset_postdata();
woocommerce_product_loop_end(); //close ul
if ( $i < count($product_categories) )
echo '<div class="content-seperator"></div>';
$i++;
}//foreach
- Try using following code and let me know if it works
$obj = get_queried_object();
if(is_shop()){
$parent_id = empty( $obj->parent ) ? 0 : $obj->parent;
}else if(is_product_category()){
$obj = get_queried_object();
$parent_id = empty( $obj->term_id ) ? 0 : $obj->term_id;
$cat_slug = empty( $obj->slug ) ? 0 : $obj->slug;
}
$excludedCats = array(47);
if($parent_id != 0){
$product_categories = get_terms( array( 'taxonomy' => 'product_cat','slug'=>$cat_slug,'exclude' => $excludedCats) );
}else{
$product_categories = get_terms( array( 'taxonomy' => 'product_cat', 'exclude' => $excludedCats) );
}
$i = 1;
foreach ($product_categories as $product_category) {
echo '<h2>'.$product_category->name.'</h2>';
woocommerce_product_loop_start(); //open ul
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
),
),
'post_parent' => $product_category->parent,
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'asc',
);
$cat_query = new WP_Query( $args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
woocommerce_product_loop_end();
if ( $i < count($product_categories) )
echo '<div class="content-seperator"></div>';
$i++;
}
I have at least 4 Parent Categories and each parent category has a sub-category.
The categories in WordPress looks like this:
Lidingö (Parent Category)
Direktåtkomst Förrådslänga(SubCategory)
7 kvm (product)
6 kvm (product)
Entréplan (SubCategory)
1 kbm (product)
1.5 kvm (product)
Nacka (Parent Category)
Sample(SubCategory)
aa (product)
bbb (product)
I want to query the products in WordPress and they should be grouped by categories.
This is my current code:
<?php
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat'
),
'posts_per_page' => 6,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo woocommerce_template_single_title();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
I think the code above is not correct because the same products are being displayed for every pagination.
Do you have any idea what is the correct query to group all woocommerce products by category? Thanks
First you need to overwrite this woocommerce template to your current theme as you have to use custom loop to get products by category.
Just copy that template of woocommerce to your current_theme->create folder name (Woocommerce) -> Paste template to that folder.
Use Below Code:
$parent_terms= get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0 ) );
if($parent_terms= )
{
foreach( $parent_terms as $parent_term )
{
$child_terms = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => $parent_term->term_id ) );
if($child_terms)
{
foreach( $child_terms as $child_term )
{
$product_args=
array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type',
'posts_per_page' => 6,
'cat' => $child_term->term_id
);
$product_query = new WP_Query( $product_args );
while($product_query->have_posts()) : $product_query->the_post();
{
Here you will get product detail so you can display product details as you wanted
}
}
}
}
}
Your query is asking for all products that belong to the product category taxonomy - that is, all of them.
You need to narrow the search by adding the category you wish to search to the arguments. For example, if you wanted to search via the category slug, you would use:
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-slug1'
),
'posts_per_page' => 6,
);
To loop through all the categories on one page, you'll want something like this:
$my_categories = get_terms( 'TERM_NAME_HERE' );
$my_categories_count = count( $my_categories );
if ( $my_categories_count > 0 && is_array( $my_categories ) ) {
echo '<div class="wrap">';
foreach ( $my_categories as $single_cat ) { ?>
<h2><?php echo $single_cat->name; ?></h2>
<?php
$cat_posts_args = array(
'post_type' => 'product',
'order' => 'ASC',
'orderby' => 'date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $single_cat->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query( $cat_posts_args );
if ( $cat_posts->have_posts() ) :
echo '<p>';
while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
<span><?php the_title(); ?></span>: <?php echo get_the_excerpt(); ?><br>
<?php endwhile;
echo '</p>';
else :
if ( !$parent ) echo '<p>No products found.</p>';
endif;
wp_reset_postdata();
} // end foreach
echo '</div>';
}
I Having trouble to exclude the current product in related product Woocommerce,
now i have change the related product from category to subcategory (without child). SO the related product will only display the subcategory product (same subcategory) but now it will also display the current product. anyone one have solution> Thank You
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$cats_array = array(0);
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
$args = apply_filters( 'woocommerce_related_products_args', array(
'exclude' => array( $cur_product_id ),
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $cur_product_id ),
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
Your problem is $cur_product_id variable!
Change with this:
$product->get_the_id()
Thank You so much. I found the solution
By adding this
$terms = wp_get_post_terms( $product->id, 'product_cat' );
and this 'post__not_in' => array( $cur_product_id ),
Here is the code
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$cats_array = array(0);
// The current product id (Woocommerce version retro compatible)
$cur_product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
$args = apply_filters( 'woocommerce_related_products_args', array(
'exclude' => array( $cur_product_id ),
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $cur_product_id ),
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
The aim is to display 4 products on the product page however remove the current product from the filter. At present I am pulling through products with related brands and categories, however this is also pulling through the current product to the related products...
Current related.php file for Woocommerce contains the following:
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$brands_array = array(0);
$cats_array = array(0);
$cur_product_id = $product->id;
// get categories
$terms = wp_get_post_terms( $product->id, 'product_brand' );
$category_terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$brands_array[] = $term->term_id;
}
foreach ( $category_terms as $category_term ) {
$cats_array[] = $category_term->term_id;
}
$final_array = array_merge($brands_array, $cats_array);
$filtered_array = array_filter($final_array, "test_odd");
function test_odd($var)
{
return($var & 1);
}
var_dump($final_array);
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'columns' => 4,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_brand',
'field' => 'id',
'terms' => $final_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php echo $filtered_array ?>
<?php woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
How do I go about filtering the current product from the array of products that are shown on the product page?
Thanks
To exclude current product, the missing argument in your WP_Query is 'post__not_in' (array). So your $args array is going to be:
$args = apply_filters( 'woocommerce_product_related_posts_query', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $product->get_id() ), // <==== HERE
'no_found_rows' => 1,
'posts_per_page' => 4,
'columns' => 4,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_brand',
'field' => 'id',
'terms' => $final_array
),
)
));
$products = new WP_Query( $args );
// . . .
See: woocommerce_product_related_posts_query replace woocommerce_related_products_args since WooCommerce 3+ (thanks to #strarsis).