Search code examples
phpwordpresspermalinks

Rewriting a GET request URL in WordPress


Any help is greatly appreciated!

I have a WordPress build that accesses information on an external database which then assists in populating the page. That structure looks like this:

http://example.com/publication/?pub=thenewyorktimes

Ideally, I would like the result to be:

http://example.com/publication/thenewyorktimes

There is a catch here - http://example.com/publication is a page which holds the API call to the external database in a WP template, so I'm also wondering if the trailing slash before the GET would make any errors. Thanks again!


Solution

  • Try using a rewrite rule:

    add_action( 'init', 'so46572689_new_rule');
    function so46572689_new_rule() {
        add_rewrite_rule('^publication/(.*)?$','index.php?pagename=publication&pub=$matches[1]','top' );
    }
    

    Make sure to flush your permalinks after you add this by going to your WP admin, Settings > Permalinks and clicking 'Save Changes'.