1

As the title suggests I am trying to create pretty permalinks for search queries, but unfortunately I cant get them to work.

I am trying to achieve the folllowing:

website.com/search/something+searched

will go to the following

website.com/?s=something+searched

I have tried the following rewrite rule within the 'init' action.

add_rewrite_rule('^search/([^/]*)/$','?search=true&s=$matches[1]','top');

This doesn't work, although 'website.com/search/something' will go to the search page but not pass the var. As does the below code:

 
function search_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        'search/(.+)'    =>  'index.php?s='. $wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'search_rewrite' );

I believe I'm missing something on my search.php file but I'm not sure what.

Is there a reason for search queries / rewrites to behave differently to other rewrites?

Thanks in advance for any assistance.

Cheers

Noel

7
  • If you pass the Query to s manually do results get returned w/o the permalinks?
    – eyoung100
    Commented May 30, 2014 at 16:32
  • the search_rewrite function would be in my functions.php file. The add_rewrite_rule was in a must use plugin
    – noelmcg
    Commented May 30, 2014 at 16:35
  • I reformed my comment after you saw it. Look again...
    – eyoung100
    Commented May 30, 2014 at 16:39
  • Thats correct. If I manually query s the results get returned as normal without returning the permalinks. I am also flushing the permalinks.
    – noelmcg
    Commented May 30, 2014 at 16:49
  • Your question is also here at the forums
    – eyoung100
    Commented May 30, 2014 at 16:59

1 Answer 1

1

Schoolboy error!

I was using:

$_GET['s'];

as opposed to:

get_query_var('s');

Hence the empty search results.

Hope this might help somebody in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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