• Resolved Overflow

    (@acrane)


    I know you have a solution already out there to include the ampersand. But it doesn’t seem to quite work all the way.

    For Example: I have search setup to search terms of a Custom Tax. One of my terms is named, “S&C Electric”

    “&” – works, returns an array of terms
    “S&” – works, returns an array of terms
    “S&C” – Doesn’t work, returns an empty array.

    Because I have an empty array, I can’t iterate through my terms to show search results.

    Any help would be greatly appriciated. Thanks.

    FYI, I also have a term, “S&D Tech” that gets the same results as above.

    https://www.ads-software.com/plugins/relevanssi/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Strange, indeed –?& and S& should not work in the first place, but S&C should.

    So you’ve added the code in that forum thread and have rebuilt the index? What exactly do you mean with the “I have search setup to search terms of a Custom Tax”?

    Thread Starter Overflow

    (@acrane)

    You can see the search results for “S&” here. But you’ll notice “S&C” does’t work.

    Products = A custom post type
    Manufacturers = Custom tax to the Products CPT

    Yes, added the ampersand code.

    Search template looks like this:

    // get what we're searching for
    $searchRequest = $_REQUEST['s'];
    
    $args = array(
        'search' => $searchRequest,
    	'hide_empty' => 0, // false, show empties
    );
    $taxonomy = 'manufacturer';
    $termsResult = get_terms($taxonomy, $args);
    
    <?php if ( !empty($termsResult)) : ?>
    	<?php  foreach ( $termsResult as $term ) { ?>
    
    <h2><?php echo $term->name ?></h2>
    <?php }
    else: ?>
    ... and the rest of the not found

    Searching “S&C” brings back an empty array for some reason.

    Thanks for you help.

    Plugin Author Mikko Saari

    (@msaari)

    That get_terms() search has nothing to do with Relevanssi. There you probably want to make sure that you have actual & and not some kind of URL encoding in $searchRequest.

    Thread Starter Overflow

    (@acrane)

    Well, that last suggestion led my to my answer, thanks.

    I added

    $new_text = str_replace('&', '& amp;', $searchRequest);  // <- no spaces in the & amp;

    to right above get_terms after the search request to change any ‘&’ to ‘&’.

    So, my final get_terms query looks like this:

    // get what we're searching for
    $searchRequest = $_REQUEST['s'];
    $new_text = str_replace('&', '& amp;', $searchRequest); // <- no spaces in the & amp;
    
    $args = array(
        'search' => $new_text,
    	'hide_empty' => 0, // false, show empties
    );
    $taxonomy = 'manufacturer';
    $termsResult = get_terms($taxonomy, $args);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Including "&" in search results’ is closed to new replies.