When 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.org/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.