I'm trying to create a agenda in a custom Wordpress theme. On the agenda page there is a simple page-agenda.php template that will include some code that handles the index of the agenda. Quite simple.

But now I would like to 'listen' to the URL that contains a detail page of an agenda item. For example /agenda/1/event-name.

When I visit the above URL I get an page not found error. And when I try to load a template file based on the init hook of wordpress, no content seems to be added to the page and I keep getting the page-not-found page.

SO. How would I load a .php template file base on a URL wildcard like /agenda/1/event-name? So I can show the event data and not get a 404 message.


  • 1
    Sounds like instead of page-agenda.php and a Page, you may be better off with a Custom Post Type of "agenda". That way you'll have archive-agenda.php as the archive instead of a Page, and single-agenda.php to handle single agendas. I'm not sure where the "1" in your desired URL is coming from, but typically you would set up /agenda/name-of-one-agenda/ style permalinks, and /agenda/ as the archive.
    – WebElaine
    Jul 3, 2018 at 19:45
  • A custom post type wouldn't be ideal in this case because the data comes from a external database. That would mean that there should be code written to sync the data with nodes in Wordpress. Might not be the most elegant solution.
    – Floris
    Jul 3, 2018 at 19:52

1 Answer 1


I ended up with this solution in functions.php, thanks to the offical Wordpress documentation at: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

First added a custom rewrite rule like so:

function custom_rewrite_rule() {
        'top' );

add_action( 'init', 'custom_rewrite_rule', 10, 0 );

Then this needed to be added:

function prefix_register_query_var( $vars ) {
    $vars[] = 'agenda_id';
    $vars[] = 'agenda_name';

    return $vars;

add_filter( 'query_vars', 'prefix_register_query_var' );

Now URL's such as /agenda/1/event-title work like a charm! And passes the variables needed.

PS: Remember to save the permalinks settings in the backend once again, or this code won't work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.