url rewriting - add_rewrite_rule ignoring other params than the "p" param

admin2025-06-06  7

A specific post on my website has a shortcode that uses a parameter called "query" to produce its content. So, this is an example of a working URL:


"my-post" is the permalink of the post and the page is using the "query" param correctly.

However, we need to change this URL to a friendly one like and then I was trying to use the add_rewrite_rule like this:

function custom_add_rewrite_rules() {
    add_rewrite_rule(
        '^something-here/([\d]+)$',
        'index.php?p=10219&query=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_add_rewrite_rules');

When I try to access the friendly URL, I'm redirected to successfully, but the query param is null ($_GET["query"] = null).

I also tried making use of add_rewrite_tag to register "query", but with no success.

If anyone has some light to shed it'd be more than appreciated.

A specific post on my website has a shortcode that uses a parameter called "query" to produce its content. So, this is an example of a working URL:

https://example/my-post?query=980

"my-post" is the permalink of the post and the page is using the "query" param correctly.

However, we need to change this URL to a friendly one like https://example/something-here/980 and then I was trying to use the add_rewrite_rule like this:

function custom_add_rewrite_rules() {
    add_rewrite_rule(
        '^something-here/([\d]+)$',
        'index.php?p=10219&query=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_add_rewrite_rules');

When I try to access the friendly URL, I'm redirected to https://example/my-post successfully, but the query param is null ($_GET["query"] = null).

I also tried making use of add_rewrite_tag to register "query", but with no success.

If anyone has some light to shed it'd be more than appreciated.

Share Improve this question asked Nov 8, 2018 at 18:33 Adriano CastroAdriano Castro 1991 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

It sounds like the plugin expects there to be a get param.

Your rewrite-rule is removing the get-param before it can be used by the plugin.

If the plugin hasn't been updated for a long time (it's abandonware) then you can go through the plugin and replace the get-param with the portion of the url which would have been the get-param. Just make sure to prevent the plugin from updating after you've made your edits.

If you add the following

function custom_add_rewrite_rules() {
  add_rewrite_tag('%query%','([^&]+)');

  add_rewrite_rule(
      '^something-here/([\d]+)$',
      'index.php?p=10219&query=$matches[1]',
      'top'
  );
}
add_action('init', 'custom_add_rewrite_rules');

& then replace $_GET['query'] &/or $_REQUEST['query] with get_query_var( 'query' ) in the plugin you've described, that should be enough to do the trick.

Stephen Harris sums it up nicely in the answer here

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749196551a317172.html

最新回复(0)