ElasticPress icon indicating copy to clipboard operation
ElasticPress copied to clipboard

Support post_parent__in and post_parent__not_in

Open ghost opened this issue 8 years ago • 6 comments

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;
}

ghost avatar Aug 24 '16 11:08 ghost

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?

ghost avatar Aug 24 '16 11:08 ghost

@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.

sc0ttkclark avatar Aug 24 '16 13:08 sc0ttkclark

+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

sc0ttkclark avatar Aug 24 '16 13:08 sc0ttkclark

@nprasath002 want to add this one to 2.5?

tlovett1 avatar Feb 13 '18 05:02 tlovett1

@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?

angtheod avatar Apr 19 '18 12:04 angtheod

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 );

angtheod avatar Apr 20 '18 10:04 angtheod