Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1279055
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI want to add a complex filtering when getting my products.
- I have products where some of them are linked to a custom post type.
- This custom post type has a repeater field.
- Depending on a cookie, I should only get products that have that value in this repeater field
I know I can use:
add_filter( 'pre_get_posts', 'fwp_archive_per_page' );
I know i can set extra meta query like this:
$query->set( 'meta_query', array(
'relation' => 'AND',
array(
'key' => 'xxx',
'value' => 'xxx'
)
));
But this is more complex as I should do a query on every product ID to check if the value exists in the repeater field from the related custom post type.
Anyone has a clue how to do this?
Or is there another way around to handle this?
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI want to add a complex filtering when getting my products.
- I have products where some of them are linked to a custom post type.
- This custom post type has a repeater field.
- Depending on a cookie, I should only get products that have that value in this repeater field
I know I can use:
add_filter( 'pre_get_posts', 'fwp_archive_per_page' );
I know i can set extra meta query like this:
$query->set( 'meta_query', array(
'relation' => 'AND',
array(
'key' => 'xxx',
'value' => 'xxx'
)
));
But this is more complex as I should do a query on every product ID to check if the value exists in the repeater field from the related custom post type.
Anyone has a clue how to do this?
Or is there another way around to handle this?
1 Answer
Reset to default 2When I have complex querie to do,
I often use a prequery to get the good post_ids.
Then I pass this list of post_ids to my main query via the post__in
parameter of WP_Query.
$meta_key = 'RepeaterName_%_FieldName';
$meta_value = 'MyCustomValue';
global $wpdb;
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT pm.post_id
FROM $wpdb->postmeta pm
WHERE pm.meta_key LIKE %s
AND pm.meta_value = %s
",
$meta_key,
$meta_value,
) );
// Or with another custom $wpdb request
// https://developer.wordpress/reference/classes/wpdb/
Then we add $post_ids
to main query
if ( !empty( $post_ids ) ) {
$post__in = $query->get('post__in');
if ( empty( $post_in ) ) {
$query->set( 'post__in', $post_ids );
} else {
$query->set( 'post__in', array_intersect( $post_ids, $post__in ) )
}
}
All this code must be placed in the pre_get_posts
filter.
I hope that the response is up to your expectations.
本文标签: loopWoocommerce Complex query in pregetposts
版权声明:本文标题:loop - Woocommerce: Complex query in pre_get_posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235193a2362872.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论