Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › limit search
This topic contains 12 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 6 months ago.
- AuthorPosts
- October 6, 2022 at 9:14 am #39533
Hello friend!
First of all: thank you so much for your great plugin. You save so much time and its a great work!
Right now we have this situation:Each post have different parents. We know we can restrict your search by parent.
But now we also need to limit the search by customfield value.Example:
If user start search in search widget TEST:
–> Show only results with parent abc OR customfield=abcSo finaly the result will only show results which parent abc or customfield value customfield=abc
I attached the code which we are using now. But how to extend it to limited search also to customfield?
We can pay you, if you say this is not very easy and we will not waste your time. 🙂BR
Tommyadd_filter( ‘asp_query_args’, ‘asp_include_only_parent_ids’, 10, 2 );
function asp_include_only_parent_ids( $args, $id ) {if ( $id == 3 ) {
$args[‘post_parent’] = array(11748);} else if ( $id == 12 ) {
$args[‘post_parent’] = array(3239);
}return $args;
}October 6, 2022 at 2:45 pm #39540Hi,
Thank you very much for your kind words!
It should not be too difficult, something like this:
add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 ); function asp_include_only_parent_ids( $args, $id ) { if ( $id == 3 ) { $args['post_parent'] = array(11748); } else if ( $id == 12 ) { $args['post_parent'] = array(3239); } $args['post_meta_filter'][] = array( 'key' => 'customfield', // meta key 'value' => 'abc' 'operator' => 'LIKE', // You can also try ELIKE here for more strict match 'allow_missing' => false // allow match if this custom field is unset ); return $args; }
You may need to try the “ELIKE” operator as well to have an exact field match, as well as try the “allow_missing” argument on true if you want results where this field does not exist.
I hope this helps!
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 6, 2022 at 3:36 pm #39542Wow, thats great. Please send me your paypal for a “thank you”. 🙂
One question i have.
How to have this please:if ( $id == 3 ) {
$args[‘post_parent’] = array(11748) OR customfield = abc;
} else if ( $id == 12 ) {
$args[‘post_parent’] = array(3239) OR customfield = xyz;So search widget with id 3 need to fit post parent id 11748 OR customfield abc.
And widget id 12 need to fit post parent id 3239 OR customfield = xyz.
Some posts are a customfield and some not.Do you can help us to solve that issue?
October 6, 2022 at 3:45 pm #39543You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 6, 2022 at 3:49 pm #39544Hello Ernest,
but in your code here
add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 ); function asp_include_only_parent_ids( $args, $id ) { if ( $id == 3 ) { $args['post_parent'] = array(11748); } else if ( $id == 12 ) { $args['post_parent'] = array(3239); } $args['post_meta_filter'][] = array( 'key' => 'customfield', // meta key 'value' => 'abc' 'operator' => 'LIKE', // You can also try ELIKE here for more strict match 'allow_missing' => false // allow match if this custom field is unset ); return $args; }
there is not this logic integrated:
If widget ID 3 –> post_parent = 11748 OR customfield = abc;
If widget ID 12 –> post_parent = 5000 OR customfield = xyz;There is a chance to archive this?
October 6, 2022 at 3:51 pm #39545Sure, it’s like this:
Best,add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 ); function asp_include_only_parent_ids( $args, $id ) { if ( $id == 3 ) { $args['post_parent'] = array(11748); $args['post_meta_filter'][] = array( 'key' => 'customfield', 'value' => 'abc' 'operator' => 'LIKE', 'allow_missing' => false ); } else if ( $id == 12 ) { $args['post_parent'] = array(3239); $args['post_meta_filter'][] = array( 'key' => 'customfield', 'value' => 'xyz' 'operator' => 'LIKE', 'allow_missing' => false ); } return $args; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 6, 2022 at 4:19 pm #39546You cannot access this content.October 6, 2022 at 4:34 pm #39551October 7, 2022 at 8:28 am #39561You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 12, 2022 at 11:05 am #39609You cannot access this content.October 12, 2022 at 5:07 pm #39622Thank you for your kind words 🙂
1. I am not a 100% sure, but from the output it seems like an incorrect post type was selected (something like “revision” or a similar name). Make sure that only the required post types are selected.
Another possible issue might be, that the post statuses are entered incorrectly. On the same options page (Search Sources -> Post type search), make sure that only the publish post status is entered.
2. Yes, it is possible, by passing the values as arrays, see this code:
Best,add_filter('asp_query_cpt', 'asp_change_query_directly', 10, 1); function asp_change_query_directly($q) { global $wpdb; return str_replace("AND $wpdb->posts.post_parent IN", "OR $wpdb->posts.post_parent IN", $q); } add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 ); function asp_include_only_parent_ids( $args, $id ) { if ( $id == 3 ) { $args['post_parent'] = array(11748); $args['post_meta_filter'][] = array( 'key' => 'ad_location', 'value' => array('62b9acbc2709e', '123', 'ABC'), 'operator' => 'ELIKE', 'allow_missing' => false ); } return $args; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 12, 2022 at 6:30 pm #396261. I am not a 100% sure, but from the output it seems like an incorrect post type was selected (something like “revision” or a similar name). Make sure that only the required post types are selected.
We have this results with all post types. I created new post type and select only this one in your plugin.
Then starting a serch. This ends also into this: Slides from elementor widgets are shown in search.Another possible issue might be, that the post statuses are entered incorrectly. On the same options page (Search Sources -> Post type search), make sure that only the publish post status is entered.
I checked this settings. We setup “publish” and we still see the elementor widget content in search result.
Last but not least:
When i remove your custom code the search does not show any content from elementor widget.
When i add your custom code into functions.php -> i see elements from slider widget again in the search.
So the issue depends to your code. You can help us if we pay for your work per hour?BR
TommyOctober 13, 2022 at 10:08 am #39636Hi Tommy,
Oh, then it was the custom code causing the issue as I predicted earlier. Changing the logic to an OR basically negated every other criteria for the search query. One last thing to try is to inject the post type and post status queries to the first code:
add_filter('asp_query_cpt', 'asp_change_query_directly', 10, 1); function asp_change_query_directly($q) { global $wpdb; $q = preg_replace('/AND ' . preg_quote("$wpdb->posts.post_parent IN ") . '\((.*?)\)/im', "OR ($wpdb->posts.post_type IN ('post', 'page', 'dealer') AND $wpdb->posts.post_status LIKE 'publish' AND $wpdb->posts.post_parent IN($1))", $q); return $q; } add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 ); function asp_include_only_parent_ids( $args, $id ) { if ( $id == 3 ) { $args['post_parent'] = array(11748); $args['post_meta_filter'][] = array( 'key' => 'ad_location', 'value' => array('62b9acbc2709e', '123', 'ABC'), 'operator' => 'ELIKE', 'allow_missing' => false ); } return $args; }
Unfortunately I am too busy to do do custom jobs, I am sorry.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.