获取给定类别ID
-
-
`category`或`product_category`?`category` or `product_category`?
- 1
- 2014-05-14
- fuxia
-
4 个回答
- 投票数
-
- 2014-06-02
我怀疑主要问题是您应该使用
WP_Query
对象,而不是get_posts()
.默认情况下,后者仅返回post_type为post
的商品,而不返回产品因此,给定ID为26的类别,以下代码将返回其产品(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);
在WooCommerce的早期版本中,可见性存储为元字段,因此代码为:
$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', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ) ) ); $products = new WP_Query($args); var_dump($products);
在这里,我们仅返回可见的产品,每页12个.
通过 http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters 有关类别定位的工作原理的更多详细信息-通常,按段检索比按ID检索更有用!
I suspect the main problem is that you should be using the
WP_Query
object rather thanget_posts()
. The later by default only returns items with a post_type ofpost
not products,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);
In earlier versions of WooCommerce the visibility was stored as a meta field, so the code would be:
$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', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ) ) ); $products = new WP_Query($args); var_dump($products);
Here we are only returning visible products, 12 per page.
Have a look through http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters for more details about how the category targeting works - it's often more useful to retrieve it by slug than by ID!
-
解决方案有效.很好的解释.Solution worked. Nice explanation.
- 0
- 2015-07-10
- Kamesh Jungi
-
从Woocommerce 3开始,可见性更改为分类而不是meta,因此您需要将meta_query更改为tax_query.参见https://wordpress.stackexchange.com/a/262628/37355.As of Woocommerce 3, visibility is changed to taxonomy instead of meta so you need to change the meta_query to tax_query. See https://wordpress.stackexchange.com/a/262628/37355.
- 1
- 2017-10-18
- jarnoan
-
您对`get_posts()`的结论是错误的.您可以在代码中将`new WP_Query($ args)`替换为`get_posts($ args)`,它将起作用.Your conclusion about `get_posts()` is wrong. You can replace `new WP_Query($args)` with `get_posts($args)` in your code and it will work.
- 0
- 2018-07-14
- Bjorn
-
-
OP专门要求使用类别ID来获取产品,但是,这对我有所帮助,因此无论如何我都会投票.请注意,它不能回答原始问题OP specifically asked for getting products using a category ID, however, this helped me, so I'll upvote anyhow. Just be aware it doesn't answer the original question
- 1
- 2019-09-24
- dKen
-
-
- 2015-01-19
通过ID或名称或子弹更改类别(类别-子弹名称)
<?php $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2,'product_cat' => 'category-slug-name', 'orderby' =>'date','order' => 'ASC' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> Within loop we can fetch Product image, title, description, price etc. <?phpendwhile;wp_reset_query(); ?>
change category (category-slug-name) by id or name or slug
<?php $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2,'product_cat' => 'category-slug-name', 'orderby' =>'date','order' => 'ASC' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> Within loop we can fetch Product image, title, description, price etc. <?phpendwhile;wp_reset_query(); ?>
-
- 2016-11-18
有点晚了,但想澄清一下事情并提供更干净的答案.用户@benz001提供了可能的有效答案,但说错了:
get_posts
返回任何类型的帖子类型,默认为posts
帖子类型,就像WP_Query
.很好地解释了这里.事实是,OP只是在
$args
数组中缺少一些参数:-
他正在搜索的帖子类型的定义:
'post_type' => 'product',
-
以及搜索查询的"分类部分"的修改:
'tax_query' => array( array( 'taxonomy' => 'product_cat', 'terms' => 26, 'operator' => 'IN', ) )
这样您的下一行
$products = new WP_Query($args); var_dump($products);
将为您显示所需的产品:)
@benz001显示的所有其他附加参数当然是有效的,但OP并没有要求,因此我决定在此答案中将其保留.
A bit late, but would like to clarify things and provide a cleaner answer. User @benz001 gave a possible valid answer, but said something wrong:
get_posts
returns any kind of post-types, defaulting toposts
post-type, just likeWP_Query
. The real differences between the two are wonderfully explained HERE.The fact is, the OP was simply missing some parameters into the
$args
array:The definition of the post-type he is searching for:
'post_type' => 'product',
And the modification of the "taxonomy part" of the search query:
'tax_query' => array( array( 'taxonomy' => 'product_cat', 'terms' => 26, 'operator' => 'IN', ) )
This way your next lines
$products = new WP_Query($args); var_dump($products);
Will show you the needed products :)
All other additional parameters shown by @benz001 are of course valid but not requested by the OP, so I decided to leave them behind in this answer.
我找不到正确的方法来获取给定类别ID(而非类别名称)的所有产品的列表.
我用来获取类别列表的代码如下,它可以正常工作:
但是,现在对于给定的类别ID(例如47),我找不到获取其相关产品的方法.我尝试了以下方法:
调试
$products
数组总是返回0,这是错误的,因为我知道ID为47的类别下有一些产品.有什么想法可以解决我的代码吗?