php – add_rewrite_rule not working with custom post type


I am trying to follow the examples in the wordpress documentation about add_rewrite_rule and add_rewrite_tag.

I’m using a custom post type called “panel“.
so the pages look like “

url queries work: for example I have 2:
var1 and testquery.

if I go to “/panel/test-page-auto/?var1=10

enter image description here

And with var1 and testquery:

enter image description here

so what i want is, change the /panel/test-page-auto/?var1=10&testquery=Hi
-> /panel/test-page-auto/10/Hello

my code:

    add_action('init', '__rewrite_tag_rule', 10, 0);
function __rewrite_tag_rule() {
     * test page tag rute
$page_type = "^panel/"; 

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

//Add rule  
    'top' ); 
//add rule for show
   'top' );


_filter(‘query_vars’, function($vars) {
$vars[] = “testquery”;
$vars[] = “var1”;
return $vars;

But it’s not working.
when I try to put /panel/test-page-auto/10/Hi it just redirects me to /panel/test-page-auto and the queries are blank.

Can someone help me understand what I’m doing wrong?

I have already tried:

  • save permalinks after each change.
  • edit the rules in various ways.
  • test the links using the queries and they are working.
Samuel Leiton 3 weeks 2022-09-07T18:28:57-05:00 0 Answers 0 views 0

Leave an answer