-
Notifications
You must be signed in to change notification settings - Fork 3
Add filter for query post result. #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Since $query is a superset of $post_query_args it is more useful as context. Also add phpdoc for filter.
4e91a81
to
ca791ef
Compare
@@ -304,7 +304,7 @@ public function process_post_query_vars( $post_query_vars ) { | |||
); | |||
|
|||
// White list allowed meta query compare values. | |||
$allowed_meta_query_compare_values = array( '=', '!=', '>', '>=', '<', '<=' ); | |||
$allowed_meta_query_compare_values = array( '=', '!=', '>', '>=', '<', '<=', 'LIKE' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sayedwp oh, I was thinking that a plugin would add LIKE
. This could be done via the customize_object_selector_post_query_vars
filter like this:
add_filter( 'customize_object_selector_post_query_vars', function( $post_query_vars, $original_post_query_vars ) {
if ( isset( $original_post_query_vars['LIKE'] ) ) {
$post_query_vars['LIKE'] = $original_post_query_vars['LIKE'];
}
return $post_query_vars;
}, 10, 2 );
In the end maybe it doesn't matter. The value is still not indexed so all of them will be slow, though LIKE
would be slower than the other comparisons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter My filter looks like this
add_filter( 'customize_object_selector_post_query_vars', function ( $post_query_vars ) {
if ( ! empty( $post_query_vars['dropdown_args']['has_some_param'] ) ) {
$search_term = $post_query_vars['s'];
$post_query_vars['meta_query'] = array(
array(
'key' => 'my_meta_key',
'value' => $search_term,
'compare' => 'LIKE',
),
array(
'key' => 'my_meta_key',
'value' => '',
'compare' => '!=',
),
);
$post_query_vars['s'] = '';
}
return $post_query_vars;
} );
where I want to search by meta key, and if LIKE
is not included in $allowed_meta_query_compare_values
it would throw error of disallowed_meta_compare_query_var
. I was not able to understand how your snippet would achieve that from a plugin. Can you explain a bit more please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sayedwp oh, you're right 😊
I suppose then that adding a customize_object_selector_allowed_meta_query_compare_values
filter would then do the trick, though this is probably overkill and it would be better to just add LIKE
to $allowed_meta_query_compare_values
like you had originally!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, that's all the changes I need, can you please merge this PR ?
No description provided.