Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Force Uppercase Search
- This topic has 16 replies, 2 voices, and was last updated 6 years ago by
Ernest Marcinko.
-
AuthorPosts
-
May 11, 2020 at 7:47 pm #27245
humblehumans86
ParticipantWe currently have the Ajax Search Pro instances implemented via a shortcode in the header of our site (visible on the provided Domain as “Quick Search”).
The plugin is configured to search Custom Post Type – Post Title (working lower and upper case assuming because display value is uppercase, slug is lowercase) and an associated Custom Field (not working lowercase). The values in said Custom Field [ _product_cpt_search_aliases ] are always stored as Uppercase values in our DB. Therefore when we search lower case we get no results.
How would you recommend forcing our input to be uppercase (not the style but the input value). Ideally we would do this with a strtoupper function in PHP rather than Javascript to avoid cursor position issues. That said we are unsure how to implement strtoupper() with the Short-code implementation of the search without altering the plugin files which wed like to avoid doing as we plan on keeping plugin versions current.
A quick test case scenario of “Quick Search” is the term “lo-16” which will return 0 results. But if you search the term “LO-16” you’ll get the results. This is because this term is found within the custom field being searched but as uppercase. Screenshots attached showing how data is stored.
May 12, 2020 at 7:29 am #27254Ernest Marcinko
KeymasterHi,
Optimally, the table and the database collation should be a case insensitive one. By default WordPress uses a case insensitive table collation, but the database collation may override that. I strongly recommend changing the collation to a case-insensitive if possible, as it may prevent other conflicts as well.
First, I suggest trying to change this option here: https://i.imgur.com/kkfz4x8.png
If that does not work, then turn it off, and instead use a custom code, like this:
add_filter( 'asp_search_phrase_after_cleaning', 'asp_map_phrase_pattern', 10, 1 ); function asp_map_phrase_pattern( $s ) { if ( function_exists('mb_strtoupper') ) { return mb_strtoupper($s); } else { return strtoupper($s); } }This will force upper case at all times.
May 12, 2020 at 12:04 pm #27258humblehumans86
ParticipantUnfortunately we have to force case sensitivity on the DB table that stores the custom field we are searching. My knucklehead clients use the same value in upper and lower case to denote a dimensions for a sister custom field. No way around it, been a huge pain to program around. A filter is perfect, thank you for the quick response will implement now.
May 12, 2020 at 12:47 pm #27259humblehumans86
ParticipantWell I stand corrected. Force Insensitivity setting did the trick despite the changes to DB collation we have on that table. You sir are are awesome, THANK YOU ten fold.
May 12, 2020 at 12:50 pm #27260Ernest Marcinko
KeymasterYou cannot access this content.
May 12, 2020 at 1:05 pm #27261humblehumans86
ParticipantRated and Commented, thanks again!
I got one more question regarding another topic, should I post as a new thread?
Question is whether or not its possbile to display associated taxonomies with the title as results rather than an image or description.
May 12, 2020 at 1:35 pm #27262Ernest Marcinko
KeymasterHi!
No need to open a new one.
Yes, it is possible, the advanced title and content fields documentation should explain everything you need.
May 12, 2020 at 1:56 pm #27263humblehumans86
ParticipantPerfect! looking at the documentation i think it’s possbile but before I go down that rabbit hole off hand do you know if its possible to only display Parent Term and Child if a Child exists? Omitting parents that have no children?
May 12, 2020 at 3:16 pm #27269Ernest Marcinko
KeymasterHi,
Unfortunately no. You can do exclude terms by their IDs, but not like that. This is only possible via custom coding, this tutorial may help.
May 12, 2020 at 3:24 pm #27271humblehumans86
ParticipantNo worries, everything else has been so easy time to roll up the sleeves!
Not only do I need to exclude Parent terms that have no Children, I need to exclude the Children term of three specific parent IDs .
Grouping Parents and Children terms isn’t possible either right?
Tried theorderby='parent' propertywithout successAssuming all three of these needs will have to be accomplished with a custom hook.
Here’s my current setting
{__tax_interchange_ct count=20 separator=', ' orderby='parent' order='ASC' exclude='21172,21174,73200' }May 12, 2020 at 3:27 pm #27273humblehumans86
Participantimg for reference. The table on the left has a section called “Interchanges”. Basically each interchange is a taxonomy of the produt listed at the top. Parent and Child denoting a Manufacture name and their corresponding Part Number for this specific Part Number listed at the top. So Ideally taxonomies are listed as Parent1 – Child, Parent -Child, etc opposed too Parent, Parent, Parent, Child, Child, Child
May 12, 2020 at 3:45 pm #27276Ernest Marcinko
KeymasterThat parameter defaults to the get_terms() wordpress internal function – which unfortunately does not support getting the terms hierarchically by default (as far as I know). The orderby=parent will order parent terms to the front, and the rest to the bottom. A similar issue is described here.
As far as I know, the only way to do that is via get_the_term_list()We have two interal functions, that you can use if you want to achieve that sorting, used as:
$categories = get_terms(array( 'taxonomy' => 'category' )); wd_sort_terms_hierarchicaly($categories, $categories_sorted); wd_flatten_hierarchical_terms($categories_sorted, $categories); // $categories variable has now the terms sorted by parent/childMay 12, 2020 at 4:05 pm #27277humblehumans86
ParticipantHierarchal sorting honestly is the most important element ignoring the other two requests. To make use of the internal functions would you wrap inside a hook in function.php?-
This reply was modified 6 years ago by
humblehumans86.
May 13, 2020 at 12:40 pm #27290humblehumans86
ParticipantErnest ,
This might be a bit of a stretch but its worth an ask. In any event appreciate all the help thus far.
Our site has two types of searches, one in the header powered by ASP and an inline page search powered by a combination of possbile of input queries. As it relates to the original task of displaying Taxonomy Terms of said search results I was able to replicate the behavior on both. So for that alone thank you again!
Furthermore I was able to implement a hierarchical display of taxonomy terms relative to each search result but with a bit of a performance loss. So instead I altered my ask and was hoping you could point me in the right direction to replicate with ASP.
Essentially I have a function in my functions.php file that accepted two parameters, a post ID and Name (though the function could determine Name, I just had value stored when I was calling function). Based on that it generates the following taxonomy terms but prevents terms from displaying based on the following criteria.
A. Taxonomy Term is the same value as the Post Title
B. Taxonomy Term is a Parent Term
c. Taxonomy Term is already being displayed and within our Term arrayHow would you recommend achieving similar result with ASP as this function only addresses our inline page search? For instance assuming I need the ‘asp_results” filter but unable to determine exactly what is being passed off in $results. Below is the function i’m using to accomplish this task with my non-ASP search.
function search_result_interchange($product_name, $product_id) { // Get Taxonomy Term IDs based on Post ID of Search Result $term_ids = wp_get_post_terms( $product_id, 'interchange_ct', array( 'fields' => 'ids', ) ); // Get Taxonomy Term objects based on applicable Term IDs $terms = get_terms( 'interchange_ct', array( 'include' => $term_ids, 'orderby' => 'term_order', 'order' => 'ASC', 'hide_empty' => true ) ); // Create array to store unique Term Names $list = array(); // Add Term Name to list if unique foreach ( $terms as $term ) { if ($term->parent > 0 && $term->name !==$product_name && !in_array($term->name, $list) ) { $list[] = esc_html( $term->name ); } } // If no unique Terms Names are found display Post Name if (!empty($list)) { echo implode( ', ', $list ); } else { echo $product_name; } }-
This reply was modified 6 years ago by
humblehumans86.
May 13, 2020 at 3:01 pm #27297Ernest Marcinko
KeymasterWell, based on that code, I constructed this for you:
add_filter('asp_results', 'asp_search_result_interchange', 10, 1); function asp_search_result_interchange($results) { // Loop through results foreach ( $results as $k => &$r ) { // Check if this is a product if ( isset($r->post_type) && $r->post_type == 'product' ) { // Get Taxonomy Term IDs based on Post ID of Search Result $term_ids = wp_get_post_terms( $r->id, 'interchange_ct', array( 'fields' => 'ids' )); // Get Taxonomy Term objects based on applicable Term IDs $terms = get_terms( 'interchange_ct', array( 'include' => $term_ids, 'orderby' => 'term_order', 'order' => 'ASC', 'hide_empty' => true ) ); // Create array to store unique Term Names $list = array(); // Add Term Name to list if unique foreach ( $terms as $term ) { if ($term->parent > 0 && $term->name !==$product_name && !in_array($term->name, $list) ) { $list[] = esc_html( $term->name ); } } // Add the imploded list of terms to the results content if (!empty($list)) { $r->content = implode( ', ', $list ) . '<br>' . $r->content; } } } }Obviously I cannot test this, but it should add the imploded terms list to the beginning of the live results contents field.
-
This reply was modified 6 years ago by
-
AuthorPosts
- The topic ‘Force Uppercase Search’ is closed to new replies.