I'm sure I've either missed something really simple, but is there a reason why this doesn't work?
global $post;
$product_id = $post->ID;
$args = array(
'post_type' => array('product_variation'),
'posts_per_page' => -1,
'post_parent' => 177,
'tax_query' => array(
array(
'taxonomy' => 'pa_billing-period',
'terms' => 'yearly',
'field' => 'slug',
'include_children' => true,
'operator' => 'IN'
)
),
);
$variations = get_posts( $args );
echo "<pre>"; print_r($variations); echo "</pre>";
I'm trying to output a specific variation but I'm getting absolutely nothing. If I remove the tax_query, all 4 variations show.
Any ideas what's missing?
$args = array(
'post_type' => array('product_variation'),
'posts_per_page' => -1,
'post_parent' => 177,
'meta_query' => array(
array(
'key' => 'attribute_pa_billing-period',
'value' => 'yearly'
)
),
);
Related
I'm using get_terms to show a list of terms. It works fine so far.
But I want to hide every term with out of stock products.
If I use 'hide_empty' => true it wouldn't work because the products are there and published.
Is there a way to add the _stock meta field to the get_terms function?
Unfortunately I have to use get_terms.
Here's my code (it's a lot bigger but that's the part):
$terms = get_terms( array(
'taxonomy' => 'product_tax',
'orderby' => 'name',
'hide_empty' => true,
) );
5 months ago but maybe it will help someone : I'm facing the same issue and all I found is to make a foreach to unset the empties values.
foreach ($terms as $key => $term) {
$args = array(
'post_type' => 'product',
'paged' => 1,
'posts_per_page' => 1,
'order' => 'DESC',
'post_status' => 'publish',
'orderby' => 'publish_date',
'meta_query' => array( array(
'key' => '_stock_status',
'value' => 'instock',
) ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $term->term_id
)
)
);
$loop = new WP_Query( $args );
if($loop->post_count < 1) {
unset($terms[$key]);
}
}
I tried many methods, but I can't show products with category name or id
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => 40,
'posts_per_page' => '12',
);
$loop = new WP_Query( $args );
Try this example,
So given a category with ID 26, the following code would return it's products (WooCommerce 3+):
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
var_dump($products);
Try tax_query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array('40'),
'operator' => 'IN',
)
)
);
$loop = new WP_Query( $args );
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Try this,
<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish','category' => 34, 'ignore_sticky_posts' => 1, 'posts_per_page' => '12',);
$products = get_posts( $args );
?>
Hope this will helps you. for more info.
I suggest used >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )
please check this screenshot >> http://prntscr.com/hjawu5
I am trying to create a short-code to get products from specific category in woocommerce. I am using 'tax_query' to target specific category and 'shortcode_atts' to get parameter from shortcode itself. The code is as follow:
function quick_launch_products($atts) {
extract(shortcode_atts(array(
'product_category_ID' => '',
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_category_ID,
'operator' => 'IN'
)
)
);
echo $product_category_ID;
$products = null;
$products = new WP_Query($args);
}
add_shortcode("quick_launch_product_slider", "quick_launch_products");
The shortcode:
[quick_launch_product_slider product_category_ID="383"]
The return value is blank. I saw a lot of demo codes and followed exactly as they were, but its not working at all.
What am i missing here?
Thanks in advance.
Here is the situation, I have a custom tax called Skill. I want to be able to display posts only with skill set as Japanese from English.
I am trying to learn how to use pre_get_posts hook to modify my get_posts query. Here is my example, however I land with the error:
Notice: Undefined variable: postdata
This is what I have tried based on research:
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' );
function wpshout_fundraiser_recent_posts( $query ) {
// Fetch only posts tagged with "Japanese from English"
$taxquery = array(
array(
'taxonomy' => 'Japanese from English',
'field' => 'skill',
'terms' => array( 'skill' ),
)
);
$query->set( 'tax_query', $taxquery );
I am sure something is wrong with the above query, which I don't fully understand. Any help and please explain what each field of the array is for if possible.
Try this below code this should work,
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' );
function wpshout_fundraiser_recent_posts( $query ) {
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'Your Post Type Name',
'tax_query' => array(
array(
'taxonomy' => 'Japanese from English',
'field' => 'skill',
'terms' => array( 'skill' ),
)
)
)
);
return $posts_array;
}
Here you can check:
$args = array(
'posts_per_page' => 5,
'post_type' => 'Post Type Name',
'tax_query' => array(
array(
'taxonomy' => 'category_taxonomy',
'field' => 'slug',
'terms' => "Category Name"
)));
$query = query_posts( $args );
while (have_posts()) : the_post();
the_content();
endwhile;
view more
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'post_type_name',
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'term_id',
'terms' => $cat->term_id,
)
)
)
);
You can try this below code :
$custom_args=array(
'post_type' => "Your Post Type Name",
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'Your taxonomy slug',
'field' => 'slug',
'terms' =>"'Your Category Name"
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<?php echo get_the_title($post->ID);?>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}
I want to display all products from multiple categories at once.
When i want to display all products from one category my $args array looks like this:
$args = array(
'post_type' => 'product',
'product_cat' => 'backpacks',
'orderby' => '_sku'
);
I remember that I can simply make an array inside my $args:
$args = array(
'post_type' => 'product',
'product_cat' => array(
'backpacks','accessoires',
),
'orderby' => '_sku'
);
But it gives me the following error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312
I know this is a simple thing but i cant figure out why its not working.
Thanks for any help!
Please try below snippet.
$sortcolumn = 'ID';
$prod_categories = array(12, 17); //category IDs
$product_args = array(
'numberposts' => -1,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'), //skip types
'orderby' => $sortcolumn,
'order' => 'ASC',
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Found a simple way to do it
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'backpacks'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessoires'
)
),
);