woocommerce Recent X commented products - wordpress

I'am looking for code in Woocommerce that allow me to display x items wth recent comments (reviews) added
simlar to [recent_products per_page="16" columns="4" orderby="rand"] shortcode but allowing to get post only with comments

I don't believe there is a shortcode for this. You'd have to write some PHP. The way that you can do this is:
$number = 5; // Change as desired
$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
if ( $comments ) {
echo '<ul class="product_list_widget">';
foreach ( (array) $comments as $comment ) {
$_product = wc_get_product( $comment->comment_post_ID );
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$rating_html = $_product->get_rating_html( $rating );
echo '<li><a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
echo $_product->get_image();
echo $_product->get_title() . '</a>';
echo $rating_html;
printf( '<span class="reviewer">' . _x( 'by %1$s', 'by comment author', 'woocommerce' ) . '</span>', get_comment_author() );
echo '</li>';
}
echo '</ul>';
}
(Note: this code is lifted with modification from the WooCommerce plugin, "recent reviews" widget: woocommerce/includes/widgets/class-wc-widget-recent-reviews.php)

This is untested, but something like this should work:
$args = array(
'post_type' => 'product',
'orderby' => 'comment_count',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if ( $post->comment_count < 1 ) continue;
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();

Related

How to get categories product in archive.php in WordPress?

I would like to display categories (from taxonomy-product_category) in archive-product.php.
Get categories:
Fruit
Herbs
Salad
Vegetables
See screenshot what I mean:
When I added coding in archive-product.php:
<?php
$args = array(
'post_type' => 'product',
'taxonomy' => 'product_category',
'hierarchical' => 1,
'nopaging' => false,
'posts_per_page' => '2',
'posts_per_archive_page' => '10',
'ignore_sticky_posts' => true,
'order' => 'rand',
);
echo '<div class="container">';
echo '<div class="row">';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="col-lg-4">';
echo '<a href="'.get_the_permalink().'">';
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(486,226));
}
the_title();
the_content();
echo '</a>';
echo '</div>';
}
} else {
// no posts found
echo wpautop( 'Sorry, no posts were found' );
}
echo '</div>';
echo '</div>';
// Previous/next post navigation.
previous_post_link( '%link', 'Prev post in category', true
);
next_post_link( '%link', 'Next post in category', true );
// Restore original Post Data
wp_reset_postdata();
?>
But not display categories (Fruit, Herbs, Salad, Vegetables)
Would anyone know about this?
Thanks,
Shaun.
Please try below code:
$args = array(
'taxonomy'=> 'product_category',
'order' => 'DESC',
);
$categories = get_categories($args);
print_r($categories);
You can get the list of all product categories using Below code
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul>';
foreach ($product_categories as $key => $category) {
echo '
<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>
';
}

custom post list item shortcode

how to add this 2 short code in one, because I want to use this in several categories. please also inform me how to add category in this short code.
now my short code is [featured-post]
I want this [featured-post='category name']
exactly, I need a shortcode 5 post will display from a category but first post have trumbanail then title then excerpt then rest of 4 list cetegory post only show title and permalink
function featured_post_shortcode($atts){
extract( shortcode_atts( array(),
$atts, 'featured-post' ) );
$q = new WP_Query(
array( 'category' => $category, 'posts_per_page' => '1', 'post_type' => 'post')
);
$list = '';
while($q->have_posts()) : $q->the_post();
//get the ID of your post in the loop
$id = get_the_ID();
$post_thumbnail= get_the_post_thumbnail( $post->ID, 'lead-thumbnail' );
$list .= '
<div class="featured">
'.$post_thumbnail.'
'.get_the_title().'
<p>' . get_the_excerpt() . '</p>
</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('featured-post', 'featured_post_shortcode');
function more_item_shortcode($atts){
extract( shortcode_atts( array(),
$atts, 'featured-post' ) );
$q = new WP_Query(
array( 'category' => $category, 'posts_per_page' => '4', 'post_type' => 'post')
);
$list = '<ul>';
while($q->have_posts()) : $q->the_post();
//get the ID of your post in the loop
$id = get_the_ID();
$list .=
'<li>' . get_the_title() . '</li>';
endwhile;
$list.= '</ul>';
wp_reset_query();
return $list;
}
add_shortcode('more-item', 'more_item_shortcode');
function featured_post_shortcode($atts){
extract(shortcode_atts( array('cat' => ''), $atts));
$q = new WP_Query(
array( 'cat' => $cat, 'posts_per_page' => '5', 'post_type' => 'post')
//Use category slug: array( 'category_name' => $cat, 'posts_per_page' => '5', 'post_type' => 'post')
);
$count = 0;
if( $q->have_posts() ):
$output = '<ul>';
while($q->have_posts()) : $q->the_post(); $count++; global $post;
if($count == 1){
$output .= '
<div class="featured">'.
get_the_post_thumbnail($post->ID, 'lead-thumbnail').
''.get_the_title().'
<p>' . get_the_excerpt() . '</p>
</div>
';
}else{
$output .= '
<li>
'.get_the_title().'
</li>
';
}
endwhile;
$output .= '</ul>';
endif;
wp_reset_query();
return $output;
}
add_shortcode('featured-post', 'featured_post_shortcode');
if you want to use the category slug then use category_name otherwise use cat which takes the category ID
in your callback: [featured-post cat="1"]

Display posts of a Sub Category of my taxonomy

I created a Shortcode to display Term of my Taxonomy. But for one of my taxonomy I have a sub term or sub category. And I don't understand how can I display posts of a Sub Category of my Taxonomy.
My Code in functions.php
function theme_lasts_posts_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
"posttype" => '',
"taxonomy" => '',
"term" => '',
"class" => '',
"exclude" => '',
), $atts));
$output = '<div class="derniersarticles">';
if ( $posttype != '' ) {
$loop = new WP_Query( array( 'post_type' => $posttype, 'taxonomy' => $taxonomy,'term' => $term, 'posts_per_page' => 100, 'post__not_in' => array($exclude) ) );
} else {
$loop = new WP_Query( array( 'post_type' => $posttype, 'posts_per_page' => 100 ) );
}
while ( $loop->have_posts() ) : $loop->the_post();
$output .= '<div class="'. $class .'">';
$output .= '<div class="thumb">';
$output .= '' . get_the_post_thumbnail() . '';
$output .= '</div>';
$output .= '<h3>' . get_the_title() . '</h3>';
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;
}
add_shortcode( 'DerniersArticles', 'theme_lasts_posts_shortcode' );
IF i understood question corectly you need to get posts from children of parent category from custom taxonomy?
In That case, try this :
<?php
$father = get_term_by('name', 'main_category_name', 'product_cat' );
$father_id = $father->term_id;
$taxonomy_name = 'product_cat';
$children = get_term_children( $father_id, $taxonomy_name );
echo '<div class="sidebar_cat">';
echo '<h5 class="sidebar_heading">'.'main_category_name'.'</h5>';
echo '<ul class="sidebar_text">';
foreach ( $children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>'.$term->name . '</li>';
}
echo '</ul>'; ?>

Woocommerce, sort dropdown on shortcode based product lists

In our shop, we have a number of standard WP pages. On these pages we show ~40 products using the standard Woocommerce shortcodes.
For example:
[product_category category="boots" per_page="20" columns="4" orderby="price" order="desc"]
The products appears, but there are two things missing:
No sorting dropdown appears above the product lists, so the products cannot be sorted by our visitors.
We don't see any pagination buttons, so it's impossible to see more than the first 20 products on each page.
Any ideas how we can fix those two things?
Regarding your first issue, I have not found any good solutions beside hacking the shortcode within WC. Not entirely advisable as it will be overwritten each upgrade/patch of WC. If it's absolutely necessary, you could maintain the code by rewriting the hack each time you upgrade.
Alright, first download a copy of includes\class-wc-shortcodes.php in your woocommerce folder. Make a backup of the original before editing it, I usually rename the file with a -o or change the file type to .bak.
Assuming you'd want the original sort by dropdown that comes with WC on the Shop Page.
Step 1
Remove orderby and order arguments on the shortcode:
[product_category category="boots" per_page="20" columns="4"]
Step 2
Edit the Product Category shortcode on class-wc-shortcodes.php as such:
/**
* List products in a category shortcode
*
* #access public
* #param array $atts
* #return string
*/
public static function product_category( $atts ) {
global $woocommerce_loop, $wpdb;
if ( empty( $atts ) ) return '';
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ) );
if ( ! $category ) return '';
// Default ordering args
// $ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order ); // COMMENT THIS OUT
$orderby = 'title';
$order = 'asc';
if ( isset( $_GET['orderby'] ) ) {
$getorderby = $_GET['orderby'];
}
if ($getorderby == 'popularity') {
$orderby = 'meta_value_num';
$order = 'desc';
$meta_key = 'total_sales';
} elseif ($getorderby == 'rating') {
$fields .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
$where .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
$join .= "
LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
";
$orderby = "average_rating DESC, $wpdb->posts.post_date DESC";
$groupby = "$wpdb->posts.ID";
} elseif ($getorderby == 'date') {
$orderby = 'date';
$order = 'desc';
} elseif ($getorderby == 'price') {
$orderby = 'meta_value_num';
$order = 'asc';
$meta_key = '_price';
} elseif ($getorderby == 'price-desc') {
$orderby = 'meta_value_num';
$order = 'desc';
$meta_key = '_price';
}
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $orderby, // $ordering_args['orderby'],
'order' => $order, // $ordering_args['order'],
'meta_key' => $meta_key,
'fields' => $fields,
'where' => $where,
'join' => $join,
'groupby' => $groupby,
'posts_per_page' => $per_page,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
'operator' => $operator
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div style="width:100%;">
<div style="float:right">
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' )
unset( $catalog_orderby['rating'] );
foreach ( $catalog_orderby as $id => $name )
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $getorderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key )
continue;
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
</form>
</div>
</div>
<div style="clear:both;"></div>
<?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(); ?>
<?php endif;
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
Upload the file back into the includes folder and you're done! Your shortcode product pages will now have a working sort by dropdown same as the one that appears on the WC Shop Page.
Edit the sort options to your liking! Hope it helps!
Well for your second problem: Your shortcode is limiting to see only 20 products. Change the it to per_page="40" and you should see 40 products or simply remove the line and the number of products is not limited.
For your first problem I don't have an answer. I'm looking it for my self as well :).
If you add the paginate="true" attribute to your [products] shortcode, then the shortcode, then the Sort by dropdown menu will appear on the page.
Change this
[product_category category="boots" per_page="20" columns="4"]
to this
[product_category category="boots" paginate="true" limit="20" columns="4"]

Tree of posts under categories and their children's categories. Function "in_category" in custom post type not working for me

I have the code that is not optimal in work - it not showing parent category post correctly. Instead of showing only the posts from the parent category he throws all posts from all children categories belonging to the parent category. I have problem with function "in_category" - is there any replacement in custom post type?? The code is below:
<?php
$categories = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => 0 , 'hide_empty'=> '0' ));
foreach ( $categories as $category ) {
if ( $category->parent > 0 ) {
continue;
}
$i = 0;
echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
$posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'CUSTOM_POST_TYPE' ) );
echo '<ul>';
foreach ( $posts as $post ) {
$child_categories = get_term_children( $category->term_id, 'MY_CUSTOM_TAXONOMY' );
if ( $child_categories && in_category( $child_categories, $post->ID ) ) {
continue;
}
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
$categories2 = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
foreach ( $categories2 as $category ) {
$j = 0;
echo '<h2>' . $category->name . '</h2>';
$posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'CUSTOM_POST_TYPE' ) );
echo '<ul>';
foreach($posts as $post) :
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
endforeach;
echo '</ul>';
}
}
?>
this is my working solution:
<?php
$args=array(
'post_type' => 'biblioteka',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'kategoria-pozycji',
'pad_counts' => false
);
$categories=get_categories($args);
foreach ( $categories as $category ) {
if ( $category->parent > 0 ) {
continue;
}
echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
$querystr = "SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
WHERE term_id = (" . $category->cat_ID . ")
AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
AND ID = object_id
AND post_type = 'biblioteka'
AND post_status = 'publish'
ORDER BY post_date DESC";
$posts = $wpdb->get_results($querystr, OBJECT);
echo '<ul>';
foreach ( $posts as $post ) {
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
$categories2 = get_terms('kategoria-pozycji',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
foreach ( $categories2 as $category ) {
echo '<h2>' . $category->name . '</h2>';
$posts = get_posts( array( 'kategoria-pozycji' => $category->name, 'post_type' => 'biblioteka' ) );
echo '<ul>';
foreach($posts as $post) {
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
}
}
?>

Resources