admin管理员组文章数量:1130349
I have this WP_Query:
new WP_Query(
array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'archive', 'membership' ),
'operator' => 'NOT IN',
),
),
'fields' => 'ids',
)
);
The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.
I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.
Thanks!
I have this WP_Query:
new WP_Query(
array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'archive', 'membership' ),
'operator' => 'NOT IN',
),
),
'fields' => 'ids',
)
);
The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.
I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.
Thanks!
Share Improve this question asked Nov 14, 2018 at 10:27 Reuven KarasikReuven Karasik 1351 silver badge6 bronze badges1 Answer
Reset to default 0My solution to this ended up splitting this into 2 queries:
- Get the IDs of all the parent products I want to ignore
Get all the variations that don't a post_parent from that list of IDs
$archived_products = wc_get_products( array( 'type' => array( 'variable', 'variable-subscription' ), 'paginate' => false, 'limit' => -1, 'category' => array( 'archive', 'membership' ), 'return' => 'ids', ) ); $my_query->set( 'post_parent__not_in', $archived_products );
I have this WP_Query:
new WP_Query(
array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'archive', 'membership' ),
'operator' => 'NOT IN',
),
),
'fields' => 'ids',
)
);
The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.
I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.
Thanks!
I have this WP_Query:
new WP_Query(
array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'archive', 'membership' ),
'operator' => 'NOT IN',
),
),
'fields' => 'ids',
)
);
The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.
I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.
Thanks!
Share Improve this question asked Nov 14, 2018 at 10:27 Reuven KarasikReuven Karasik 1351 silver badge6 bronze badges1 Answer
Reset to default 0My solution to this ended up splitting this into 2 queries:
- Get the IDs of all the parent products I want to ignore
Get all the variations that don't a post_parent from that list of IDs
$archived_products = wc_get_products( array( 'type' => array( 'variable', 'variable-subscription' ), 'paginate' => false, 'limit' => -1, 'category' => array( 'archive', 'membership' ), 'return' => 'ids', ) ); $my_query->set( 'post_parent__not_in', $archived_products );
本文标签: woocommerce offtopicQuery child posts with tax query on parents
版权声明:本文标题:woocommerce offtopic - Query child posts with tax query on parents 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749179518a2328361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论