ElasticPress
ElasticPress copied to clipboard
Support post_parent__in and post_parent__not_in
While post_parent
, post__in
and post__not_in
are now supported, why don't integrate also post_parent__in
and post_parent__not_in
?
I think, a code similar to this could be implemented:
if ( ! empty( $args['post_parent__in'] ) ) {
$filter['and'][]['bool']['must'] = array(
'terms' => array(
'post_parent' => array_values( (array) $args['post_parent__in'] ),
),
);
$use_filters = true;
}
if ( ! empty( $args['post_parent__not_in'] ) ) {
$filter['and'][]['bool']['must_not'] = array(
'terms' => array(
'post_parent' => array_values( (array) $args['post_parent__not_in'] ),
),
);
$use_filters = true;
}
Subsidiary question: why in post__in
filter line 1022, $args are formated with array_values
, and not in post__not_in
filter line1037? The arguments are quite passed the same way, isn't it?
@r66r array_values ensures that any weird arrays coming in from WP_Query requests don't interfere with ElasticSearch requests. If what is supposed to be an array of values comes in with anything other than a 0,1,2,3,4 keyed array, then it turns into an object
and the request causes ElasticSearch to return failures.
+1 to add this though, I would have but I didn't need it at the time when I added post__in / post__not_in
@nprasath002 want to add this one to 2.5?
@r66r @tlovett1 Hello and thanks for your great work! Is there any workaround for post_parent__not_in that you could suggest until this is added to the plugin?
nevermind, figured it out
add_filter( 'ep_formatted_args', function ( $formatted_args ) use ( $args ) {
if ( ! empty( $args['post_parent__not_in'] ) ) {
$formatted_args['post_filter']['bool']['must_not'] = array(
'terms' => array(
'post_parent' => array_values( (array) $args['post_parent__not_in'] ),
),
);
$use_filters = true;
}
return $formatted_args;
}, 10, 1 );