0

In functions.php I have the following filter:

function checkForAa($query){

    if($query->is_search){

        $search_string = get_query_var( 's' );

        if( stripos( $search_string , 'aa' ) !== false){
            $new_string = str_ireplace( 'aa' , 'å' , $search_string );
        }elseif( stripos( $search_string , 'å' ) !== false){
            $new_string = (str_ireplace( 'å', 'aa' , $search_string ));
        }

        if( !empty( $new_string ) ){
           $query->set( 's2', $new_string);
        }

    }
}

add_filter('pre_get_posts', 'checkForAa');

However, when the $new_string is not empty, it is not added as a new search string.

Any help or guidance would be much appreciated.

  • What is s2? Won't a simple else solve the problem? – s_ha_dum Oct 20 '15 at 13:36
  • That's is another search string. Basically, what I need is if $new_string is not empty, to look for both strings. – Domas Oct 20 '15 at 13:44
  • WordPress doesn't use s2, so either you've made changes not explained above or this will never work at all as WordPress Core will just ignore the value. – s_ha_dum Oct 20 '15 at 14:01
2

I am not 100% sure what you are trying to accomplish, but you can "search for both strings" simply by concatenating them:

function checkForAa($query){

    if($query->is_search){

        $search_string = $query->get( 's' );

        if( stripos( $search_string , 'aa' ) !== false){
            $new_string = str_ireplace( 'aa' , 'xxx' , $search_string );
        }elseif( stripos( $search_string , 'å' ) !== false){
            $new_string = (str_ireplace( 'å', 'aa' , $search_string ));
        }

        if( !empty( $new_string ) ){
           // Concatenate the strings
           $query->set( 's', $new_string.' '.$search_string);
        } 

    }
}

add_filter('pre_get_posts', 'checkForAa');

$s = 'aabcd';
$query1 = new WP_Query( 
  array( 
    'posts_per_page' => 3, 
    's' => $s
  ) 
);
var_dump($query1->request);

This kind of search will become very inefficient, very quickly, with long strings though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.