0

I'm losing my mind, so I do hope I can get some help here..

What I'm trying to do is to create an optional parameter for search page, just for the sake of SEO

For example, there are the options:

/search/ (category = null)

/search/contracts (category = contracts)

/search/.../ (category = whatever this parameter $1 is)

While search is a page

I've tried adding htaccess rewrite rule, and wordpress simply redirected 301 without passing the parameter. Then I tried with add_rewrite_endpoint, nothing worked whatsoever and I've tried with

function wpd_add_query_vars( $qvars ) {
  $qvars[] = 'category';
  return $qvars;
}
add_filter( 'query_vars', 'wpd_add_query_vars' );

add_rewrite_rule(
    '^search/([^/]+)/?',
    'index.php?post_id=806&category=$matches[1]',
    'top'
);

add_rewrite_tag('%category%', '([^&]+)');

flush_rewrite_rules();

Do note I tried just about every permutation, also tried plugins, nothing worked. It was either a 404 or the $wp_query->query_vars did not include the parameter.

Thanks in advance..

  • What is post ID 806? post_id isn't a valid query var. add_rewrite_rule should also be hooked to run on the init action. Rewrite rules have to result in a valid main query, so your best bet is to start with creating a new WP_Query that gives you the results you are trying to achieve, then work backwards from that to derive your rule. – Milo Mar 20 at 3:18
  • I've tried also with pagename and p, but 806 is page search. I'll try with WP_Query, thanks for the input. But I now I'm seriously doubting going the WP way will take less time than vanilla – alps Mar 21 at 10:28

Your Answer

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

Browse other questions tagged or ask your own question.