php – add_rewrite_rule not working with custom post type

Question

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 “site.com/panel/post-slug

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  
//panel/test-page-auto/10/edit
add_rewrite_rule( 
    $page_type."test-page-auto/([^/]*)/([^/]*)/?", 
    'index.php?pagename=test-page-auto&var1=$matches[1]&testquery=$matches[2]',
    'top' ); 
//add rule for show
//panel/test-page-auto/10    
    add_rewrite_rule( 
        $page_type."test-page-auto/([^/]*)/?", 
        'index.php?pagename=test-page-auto&var1=$matches[1]&testquery=100edit',
   'top' );
    }

    add

_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.
0
Samuel Leiton 3 weeks 2022-09-07T18:28:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse