I've been looking for the answer to this everywhere but with no success and that's why I'm asking here.

I don't even know if the question is the right one.

In any case, I'll try to explain.

I have a WordPress page with variables in the URL, like this: http://planeta-beisbol.com/tal-dia-como-hoy/?dia=18&mes=04

The content changes depending on the day and month of the year because is gathered from the databse.

My question is: How can I change the URL to look something like planeta-beisbol.com/tal-dia-como-hoy/dia/18/mes/04 or planeta-beisbol.com/tal-dia-como-hoy/18/04

Any help is apreciated. Thanks.

  • I have a couple questions that may help someone else answer this better. In your link structure do you mean something like this: planeta-beisbol.com/ tal = tagname? - dia = tagname? -como-hoy/dia = category? /18/mes/04 D/M/Y? or would it be planeta-beisbol.com/ tal-dia-como-hoy post name? / dia CPT/Category/Term /18/mes/04 D/M/Y Identifying these may help someone better help you with building your Query – David Apr 18 '12 at 9:03
  • "tal-dia-como-hoy" is the page name, something like "this date in history". Meaning: planeta-beisbol.com/tal-dia-como-hoy ... This page has some code that takes from the variables in the URL to pull content, which is based on the current day and month, but also the visitors can choose any other date to read some historic happenings in baseball ... I would like to have the URL in permalink form rather than "?dia=18&mes=04" ("dia" means day, and "mes" means month") – FrankPereiro Apr 18 '12 at 12:47
  • If it's easier to make a template of it instead of a standard page, that would be fine with me... Thanks for answering Dave, I hope what I've said could clear things. English is not my mother language, sorry. – FrankPereiro Apr 18 '12 at 12:47
  • Frank, I hope you've gotten a little closer. I only asked because sometimes it's easy for anyone to misinterpret what is asked. There's a few options out there for rewriting but it looks like you're on the right track with the answer below ;) – David Apr 18 '12 at 14:00

The following two functions should point you in the right direction:

function wpse49393_query_vars($query_vars)
    $query_vars[] = 'myvar';
    return $query_vars;
add_filter('query_vars', 'wpse49393_query_vars');

function wpse49393_rewrite_rules_array($rewrite_rules)
    $rewrite_rules['path/to/page/([^/]+)/?$'] = 'index.php?pagename=path/to/page&myvar=$matches[1]';
    return $rewrite_rules;
add_filter('rewrite_rules_array', 'wpse49393_rewrite_rules_array');

I would give credit where it is due, but I cannot remember where I originally found this code. I just pulled this out of a project that I am currently working on. To use it, change myvar to the name of your variable in the first function as well as here: &myvar=$matches[1]. Change path/to/page/ to the actual path of your page at two locations in the second function.

| improve this answer | |
  • OK, so far I got this code: code function add_directory_rewrite() { add_rewrite_tag("%dia%", '(.+)'); add_rewrite_tag("%mes%", '(\d+)'); add_rewrite_rule('^tal-dia-como-hoy/(.+)/(\d+)', 'index.php?p=3393/?dia=$matches[1]&mes=$matches[2]', 'top'); } add_action( 'init', 'add_directory_rewrite' ); code and for testing, I got this code in my page: code echo get_query_var("dia").'/'.get_query_var("mes"); code In my page it retreaves correctly "18/04" but if I follow this URL "planeta-beisbol.com/tal-dia-como-hoy/18/04" it sends me straight to my homepage – FrankPereiro Apr 18 '12 at 16:14
  • I'm sorry, I don't know how to display code here – FrankPereiro Apr 18 '12 at 16:20
  • Remove that code and try what I suggested. – Joseph Leedy Apr 18 '12 at 18:42

You can do you're own custom permalink structures, have you looked here - http://codex.wordpress.org/Using_Permalinks?

| improve this answer | |

Thanks to this post for the heads up: http://zagar.biz/2011/wordpress-creating-custom-permalinks/

My final code ended up being like this:

In the theme's functions.php file


// Add all rewrite rules here
// ** Remember to Flush Rewrite rules when adding new rules.
// this is done by visiting the Settings > Permalinks in WP admin.

function mycode_add_rewrite_rules($rules){
    $newrules = array();
    $newrules['tal-dia-como-hoy/([^/]+)/([^/]+)/?'] = 'index.php?pagename=tal-dia-como-hoy&dia=$matches[1]&mes=$matches[2]';
    return $newrules + $rules;


// Add all slugs so that WP recognizes it
function mycode_add_rewrite_query_vars($vars){
    array_push($vars, 'dia');
    array_push($vars, 'mes');
    return $vars;

Then, in the page I created. This is just a regular wordpress page, not a template or anything like that

global $wp;

// connect to your external DB, etc...


echo "Permalinks are working";
    // load up your external data here and display single resource
        // if the beard slug doesn't exist, redirect to 404
} else {
    // load up whatever you want to display for the index version.
echo "Nothing shows up but it's working anyway";

I have to edit the page to make it look good with my brand new permalinks.

This is the page I'll using with this code: http://planeta-beisbol.com/tal-dia-como-hoy/19/04/

The "19/04" part means: 19th of April

I hope this code is useful for everybody

| improve this answer | |
  • 1
    That code is pretty much what I suggested in the first place. Why did you ignore me? – Joseph Leedy Apr 19 '12 at 14:13
  • I agree with @Joseph – Brian Fegter Sep 25 '12 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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