I am trying to get all products with a specific category. It shows the data if I remove the product category code from the code else doesn't show anything. I am sure I am using the right product category.
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'men'
),
),
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$tre = get_post_meta(get_the_ID(), '_xoo-wl-users', true );
$data = json_decode($tre);
foreach($data as $key => $value)
{
echo $key;
echo "<br>";
}
}
}
Use below code in $args so it will display products only of men category
<?php $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'men', 'orderby' => 'date' ); ?>
Try this :
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'men', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
The reference here: https://wordpress.stackexchange.com/questions/67247/how-to-display-product-specific-to-a-category-with-woocommerce-plugin
Related
I apologize for typing.
I've categorized a word in WordPress in the post category section
And I've made exactly the same thing in product categories
There is no plugin or code that automatically displays related product categories in the post?
WooCommerce - Show products from current product category (when
viewing a single product)
<?php
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
if ($r->have_posts()) {
?>
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>
</ul>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_query();
}
}
?>
This a part of a program to display a product list. I created the products as custom posts, and I need to display the posts on the basis of taxonomies.
Who can I provide taxonomies as an argument in post selection arguments?
I created taxonomies and posts using tool set plugin.
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<h1>
<?php echo $taxonomy->name;
//$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
//how can i add taxonomies asan argument here
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>
Here is the working code
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<div class="product-row clearfix">
<div class="bg-box">
<?php if($i==1){?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-guitar"/>
<?php }else if($i==2){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-speaker"/>
<?php }else if($i==3){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-headphone"/>
<?php } ?>
<a href="<?php echo esc_url( add_query_arg( array('cat'=>$taxonomy->term_id), home_url('/products') ) ); ?>" class="view-more-btn button button--moema ">
READ MORE
</a>
</div>
<div class="items-list-box">
<h1>
<?php echo $taxonomy->name;
$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'product_category',
'field' => 'name',
'terms' => $ir,
)
),
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<!-- <img src="<?php //echo esc_url($pr_img_source); ?>" alt="<?php the_title(); ?>">
-->
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>
I have a post type called 'dining' and has a taxonomy called 'dining-category'.
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
In WordPress 4.6 get_terms is deprecated. So there is an alternate of this (get_categories) Read this
And here is Example code:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><?php echo $category->name; ?></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><?php echo $wcatTerm->name; ?></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.
Can someone help me how to display the post only for a week? i have this code that works fine in post_date:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?
Thanks.
This Might help you to get solution:
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The previous answer was along the good track. Just modify your date_query:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
I have displayed the custom post type in the widget. Now i want to add pagination in the last. because i have more than 10 posts in my custom post type.
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
Try this one and enter your custom post type's name in 'post_type' => 'your custom post type name'
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'paged' => $paged1,
'post_type' => 'your custom post type name'
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
?>
<?php endif;
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $previous_post->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
wp_reset_postdata(); ?>
</ul>