Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Frontend Search Settings: Any post type
This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko 2 years, 1 month ago.
- AuthorPosts
- January 25, 2021 at 10:36 am #31272
Dear support,
is there any way to use an “Any” field in Frontend Search Settings for Post type?
The customer wants the form to have these filters:
Any post type (Post type 1, Post type 2, Post type 3 … Post type n) | Post type 1 | Post type 2How it should work
1. If Any post type is checked, it should search in any post type that is setup in sources (including Post type 1 and 2).
2. the Post type 1 and 2 should not be checked if Any is checkedA temporary workaround might be to simply display all as checkboxes, relabel “Select all” as Any, but:
1. you have to bind events to checkboxes to deselect them if Post type 1 or Post type 2 will be selected
2. you have to hide things with CSS
3. clicking any deselects allIs there any other way to do this?
Best regards
January 25, 2021 at 2:10 pm #31279Hi,
If I understand correctly, you want to divide the post type selectors to multiple sections, and some options (post type 1 and post type 2) should work sort of independently from the “Any” selector?
Best,
While there is an Select All/Any option integrated with the post type selectors, this is not possible without custom coding for sure. I’m afraid there is no other way of doing this.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 26, 2021 at 7:55 am #31287Hello,
ok, that’s unfortunate.
One of the ideas was also changing options to radio buttons and adding a filter to asp_pre_get_front_filters with:
array( 'field' => 'cpt', 'value' => 'any', // or '', or 'post_type1,post_type2,post_type3' 'label' => 'Any', 'default' => true, 'selected' => true )
But none of this is recognized and it just fallbacks to “post”. Is there anything else we can try in the value field, or it just allows single value with one of the post_types selected in source?
Best regards
January 26, 2021 at 1:41 pm #31290In theory, maybe by adding “fake” filters to the front-end with any labels you choose, then processing them during the query arguments filter could actually work.
So for example:
add_filter('asp_pre_get_front_filters', 'asp_add_a_filter', 10, 2); function asp_add_a_filter($filters, $type) { foreach ($filters as $k => &$filter) { if ( $filter->type() == 'post_type' ) { $filter->add(array( 'label' => 'My Post Type', 'selected' => false, 'value' => 'my_post_type' )); } } return $filters; }
This should add an additional filter to a post type filter with a label “My post Type” and value “my_post_type”. When this is selected, it is passed to the query arguments.
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { // The my_post_type was selected if ( in_array('my_post_type', $args['post_type']) ) { $args['post_type'] = array('post', 'page', 'other_post_type'); } return $args; }
This then detects if the ‘my_post_type’ was selected, and changes the post type arguments to whatever you need.
In theory this should work, but I honestly don’t recall if there is any existence check before or after the query arguments for post types – in which case this can be an issue, and probably will not work.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 27, 2021 at 9:05 am #31300Hello,
thanks! This works perfectly.
While trying to set a position for this item, we have discovered a possible bug. If you set position parameter to anything, it will throw an Warning:
Warning: array_splice() expects parameter 1 to be array, null given in /ajax-search-pro/includes/classes/frontend/filters/class-asp-filter.php on line 64
$original is NULL
This issue is present in 4.20.3
Best regards
January 27, 2021 at 10:30 am #31304Hi,
Thank you for that, I will correct that in the next release. We are about to release it today fixing some other issues with 4.20.3, feel free let me know if the issue persists in 4.20.4 once it’s released.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 27, 2021 at 12:27 pm #31306Hello,
installed, but it doesn’t correctly splice the values. The code should probably be:
array_splice( $this->values, $position, 0, array($new) );
If the last value is not an array it is typecasted to one which means that the structure is not preserved.
Here is one last question about the original problem: is it possible to also override parameters for the search page results? Ajax load works great, but on search page the above filter seems to be ignored.
Best regards
January 27, 2021 at 1:54 pm #31309Thanks, I will rewrite the unit tests on that, somehow it slipped through even with typecasting correctly.
If you mean when clicking the magnifier or hitting the return button to redirect to the results page, then make sure the search override feature is enabled.
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.