Skip to content

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

Merged
merged 3 commits into from
Feb 2, 2017
Merged

Conversation

mohdsayed
Copy link
Contributor

No description provided.

Since $query is a superset of $post_query_args it is more useful as context. Also add phpdoc for filter.
@westonruter westonruter force-pushed the add-filter-for-results branch from 4e91a81 to ca791ef Compare January 23, 2017 19:46
@@ -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' );
Copy link
Contributor

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.

Copy link
Contributor Author

@mohdsayed mohdsayed Feb 2, 2017

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?

Copy link
Contributor

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!

Copy link
Contributor Author

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 ?

@westonruter westonruter merged commit 00cbf86 into develop Feb 2, 2017
@westonruter westonruter deleted the add-filter-for-results branch February 2, 2017 18:28
@westonruter westonruter modified the milestone: 0.4.1 Feb 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants