admin管理员组文章数量:1291124
We've got a webshop with over 3000 products. Some products have the same attribute value for pa_model, as shown below.
Productid 1 - pa_model Nike
Productid 2 - pa_model Nike
Productid 3 - pa_model Nike
Productid 4 - pa_model Nike
Productid 5 - pa_model Nike
Productid 6 - pa_model Nike
Productid 7 - pa_model Nike
Productid 8 - pa_model Adidas
Productid 9 - pa_model Adidas
Productid 10 - pa_model Adidas
Productid 11 - pa_model Adidas
Productid 12 - pa_model Puma
Productid 13 - pa_model Puma
Productid 14 - pa_model Puma
Productid 15 - pa_model Puma
Productid 16 - pa_model Puma
Inside the pre_get_posts hook we want to loop through all products from Woocommerce but only get first product with pa_model Nike and skip all products where pa_model is the same, so the outcome will be:
Productid 1 - pa_model Nike
Productid 8 - pa_model Adidas
Productid 12 - pa_model Puma
The reason why we're trying to do this inside the pre_get_posts hook, is if we're trying to do this outside the regular loop with a wp_query we're getting a timeout because it exceeds the maximum time limit.
We want to use posts_per_page (15 for example) in our loop, but the ignored products with the same pa_models can't count as a post_per_page.
$query->set('post_type', 'product');
$query->set('posts_per_page', 15);
$query->set(**TAXONOMY QUERY HERE**);
本文标签: Tax query get first product with attribute value in pregetposts
版权声明:本文标题:Tax query get first product with attribute value in pre_get_posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516775a2382938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论