This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Force Uppercase Search

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #27245
    humblehumans86humblehumans86
    Participant

    We 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.

    #27254
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    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.

    #27258
    humblehumans86humblehumans86
    Participant

    Unfortunately 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.

    #27259
    humblehumans86humblehumans86
    Participant

    Well 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.

    #27260
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

    #27261
    humblehumans86humblehumans86
    Participant

    Rated 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.

    #27262
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    No need to open a new one.

    Yes, it is possible, the advanced title and content fields documentation should explain everything you need.

    #27263
    humblehumans86humblehumans86
    Participant

    Perfect! 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?

    #27269
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    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.

    #27271
    humblehumans86humblehumans86
    Participant

    No 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 the orderby='parent' property without success

    Assuming 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' }

    #27273
    humblehumans86humblehumans86
    Participant

    img 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

    #27276
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That 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/child
    #27277
    humblehumans86humblehumans86
    Participant

    Hierarchal 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?

    #27290
    humblehumans86humblehumans86
    Participant

    Ernest ,

    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 array

    How 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;
      }
    
    }
    
    #27297
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Well, 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.

Viewing 15 posts - 1 through 15 (of 17 total)
  • The topic ‘Force Uppercase Search’ is closed to new replies.