I need to convert domain.com/agent/99/name to domain.com/agent/agentid=99 in wordpress. The site has a page name/slug “agent” (page_id=1882) and it must be pretty permalink enabled.

My code works fine when pretty permalink disabled. But as soon as I enable pretty permalink (Post name) rewrite fails to work. I have tried both .htaccess and add_rewrite_rule.

Using .htaccess I tried this code below:

RewriteEngine on
RewriteRule ^agent/([0-9]+)/(.+)$ index.php?page_id=1882&agentid=$1 [NC,L]

Using add_rewrite_rule I tried this code below:

add_action('init', 'rewriteAgentLink');  
function rewriteAgentLink()
{   
    add_rewrite_rule(
        "agent/([0-9]+)/(.+)/?$",
        "index.php?page_id=1882&agentid=$matches[1]",
        "top"
    );
    global $wp_rewrite;
    $wp_rewrite->flush_rules(true);
}

Read more here: How to add rewrite rule to convert domain.com/agent/99/name to domain.com/player/playerid=99 in wordpress?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: