I am a little confused as to that the issue is here, I am trying to pick out a specific product by doing this:
<?php
ini_set('max_execution_time', 0); //I saw maximum execution time error on your image - this is for that
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_value' => 'yes',
'posts_per_page' => 10,
'product_cat' => 'grammar'
);
$product_query = new WP_Query( $args );
?>
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); global $product; ?>
<?php the_title(); ?>
<?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ); ?>
<?php endwhile; ?>
But nothing is being produced. So I am creating a product under the relevant category and placing content into the description in both main and product short description but still nothing shows?
The error is probably 'meta_value' => 'yes',. You need to specify a meta_key as well.
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => 'my_meta_key',
'meta_value' => 'yes',
'posts_per_page' => 10,
'product_cat' => 'grammar'
);
I have no idea what the meta key should be so change my_meta_key into what you want. Also make sure that the the value for product_cat is correct. It should be the slug of the category.
Related
I'd like to exclude the category of my current post from the loop. Pretty easy usually, this time it doesn't work and I can't figure out what's wrong here.
Here's my page'code:
$postid = get_the_ID(); // curret product ID
<section class="related products">
<?php
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'cat' => '-33' // Exclude cat
);
$related_products = new WP_Query($args);
?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php if( $related_products->have_posts() ) {
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; }
?>
</section>
End of page
<?php
wp_reset_postdata();
?>
It shows all products (except the displayed one, which is correct).
Do you have any suggestion?
Try with the following additional tax_query, as product categories are a custom taxonomy:
<?php
$related_products = new WP_Query( array(
'post__not_in' => array( get_the_ID() ), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( 33 ), // HERE the product category to exclude
'operator' => 'NOT IN',
) ),
) );
if( $related_products->have_posts() ) : ?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
?>
</div>
<?php endif; ?>
You can get current post's category using get_the_category
and you can exclude categories using category__not_in in your argument.
So your argument should be like bellow
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'category__not_in' => get_the_category(get_the_ID())//exclude category of current post
);
Try this then let me know the result. Thanks
I am using the php below in an enhanced text widget to display a list of recently added products. It works fine.
However, my shop also has free products (Price 0), and I do not want these displayed in the list.
What to I need to add to the code to exclude free products?
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />'.get_the_title().'';
endwhile;
wp_reset_query();
?>
You could try with wp_query only
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 8,
'meta_query' => [[
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
]]
) );
Check here for more examples.
So, I am trying to show items from a specific category in the woocommerce:
Here is what I have so far:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => '', 'orderby' => 'date', 'order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<div class="content"> Content </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
When the product_cat is empty, it shows all the items. I want to include "exclude category".
For example, I want to show all but items in a "no_good" category.
Could someone help me out with it?
Also, how can I add a pagination to this?
Thanks!
Is product_cat your custom taxonomy? If it is then you need to modify your $args with tax query:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'tax' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array('no_good'),
'operator' => 'NOT IN',
),
),
);
...
This assumes that "no_good" is a product_cat name. Adjust field if it isn't.
Regarding the pagination part, do check the codex article regarding pagination.
I want to show the top selling products in WooCommerce for the last 30 days. Just to show the top sellers overall is no problem with the provided code.
What the code is missing is the part where you get sales in a certain period of time. The meta_query should be the key to do this but I'm not sure how. Any help is appreciated!
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
You can use this short code for best selling product in Woo commerce.
[best_selling_products] in page edit.
OR
Also add on php file where you want like
do_shortcode('[best_selling_products per_page="12"]');
Just Change your code like below
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_key' => 'total_sales',
'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' )
);
Hope its work.
I am retrieving some post and to show post on first come first basic. Now I want to show those post on the top which are posted by admin and the rest post by non admin will come after admin posts.
the code I am using in php is:
$tit = get_the_title();
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
);
$slides = get_posts($args);
?>
<ul id="myList">
<?php foreach($slides as $post) : setup_postdata($post); ?>
<li>the post will go here</li>
<?php endforeach; wp_reset_postdata(); ?>
To display posts from admin first and then from non-admin, you have to call get_posts twice.
Once with :-
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
'author' => '123' // where 123 is ID of your admin author
);
The other will be :-
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
'author' => '-123' // display posts except admin author
);
Add the author method to your array:
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit,
'author_name' => 'Administrator' //add this, change Administrator to your name
);