I get product by:
$args = array(
'post_type' => 'product',
'posts_per_page' => 15,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
)
);
$post= new WP_Query( $args );
I want to get products but order by view so I did:
$args = array(
'post_type' => 'product',
'posts_per_page' => 15,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
),
'order' => 'ASC', // add this
'suppress_filters' => false, // add this
'orderby' => 'post_views' // add this
);
$post = new WP_Query( $args );
But still show the same result, any idea?
Not sure if this helps but instead of 'ASC' try 'DESC'?
Related
I use the array to show all products and posts on the same page:
$args = array(
'post_type' => array( 'product', 'post' ),
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC'
);
$custom_query = new WP_Query($args);
Now I want to filter posts 'cat' => '82,85' and products 'product_cat' => 96.
I have added the code snippes to my code, but after no post or products are shown.
$args = array(
'post_type' => array( 'product', 'post' ),
'cat' => '82,85',
'product_cat' => 96,
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC'
);
$custom_query = new WP_Query($args);
How should I modify my code that both post types and all posts and products within the categories will be displayed?
Thank you,
Mags
You can do that by using tax_query.
$args = array(
'post_type' => array('product', 'post'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => ['82', '85'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => ['96'],
),
),
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC',
);
$custom_query = new WP_Query($args);
I made a category selection section that can be used to specify the woocommerce product category and then display the products of that category.
$args = array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => intval($_POST['category']),
'operator' => 'IN'
)
));
$id_posts = get_posts($args);
But I want to display all products when a category is not selected.How do I value 'terms' to achieve my goal?
you can define a function that checks all your categories in order and then gives you the ID of the categories.
Now you can put these ideas in terms.
function get_all_ID_category()
{
$args = array(
'taxonomy' => "product_cat",
'number' => 0,
'orderby' => 'name',
'order' => 'ASC',
);
$product_categories = get_terms($args);
$product_cat = [];
foreach ($product_categories as $pc) {
$int_pc = $pc->term_id;
$product_cat[] = $int_pc;
}
return $product_cat;
}
function get_all_posts_category()
{
$args = array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'fields' => 'ids',
'terms' => get_all_ID_category(),
'operator' => 'IN'
)
)
);
return get_posts($args);
}
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 use woocommerce plugin.
I have products listed by category. I need to list them by price.
I tried this code below :
$args = array(
'post_type' => 'product',
'meta_key' => '_price',
'orderby' => 'meta_value_num',
'order' => $order,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cid
)
)
);
$my_query = new WP_Query( $args );
The result is not sorted by price, only by id.
Is there any solution?
products listed by category
<?php
$products_category_object= get_queried_object();
$product_category_taxonomy= $products_category_object->taxonomy;
$product_category_term_id= $products_category_object->term_id;
$product_category_name= $products_category_object->name;
$product_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => '_price',
'orderby' => 'meta_value_num', //meta_value Or meta_value_num
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $product_category_taxonomy,
'field' => 'id',
'terms' => $product_category_term_id
)
),
);
$product_my_query = null;
$product_my_query = new WP_Query($product_args);
if( $product_my_query->have_posts() )
{
while ($product_my_query->have_posts()) : $product_my_query->the_post();
echo get_the_title( $post->ID );
endwhile;
}
?>
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'
)
),
);