How can I turn a URL like:





I was looking the rewrite doc here: http://codex.wordpress.org/Class_Reference/WP_Rewrite#Examples

and came up with this

// Adding a new rule
function my_insert_rewrite_rules( $rules )
  $newrules = array();
  $newrules['(project)/(\d*)$'] = 'index.php?project=$matches[2]';
  return $newrules + $rules;

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
    array_push($vars, 'project');
    return $vars;

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp', 'test');

function test()
 global $wp_rewrite;
   echo $_GET['project'];


but it doesn't work when I access for example http://site.com/project/2/

  • what is a project? a custom post type?
    – Milo
    May 7, 2012 at 17:56
  • no. it's nothing, just a query variable i'm using
    – Alexa
    May 7, 2012 at 17:56
  • you can pass project as a query var, but your destination has to be something, like a page.
    – Milo
    May 7, 2012 at 19:33

2 Answers 2


The PHP code is not enough, you need to edit your .htaccess file correspondingly. (You can do this with $wp_rewrite->mod_rewrite_rules(); somehow, or edit it manually.) However, be aware that URL rewriting is complicated stuff - are you sure you need it? If you are using pretty permalinks for posts/pages, chances are this will bork it up.


The simple way to do this is to create a page, and give it the slug project.

Then add your rewrite rule and query var:

add_action( 'init', 'my_insert_rewrite_rules' );
function my_insert_rewrite_rules(){
        'top' );

add_filter( 'query_vars', 'my_insert_query_vars' );
function my_insert_query_vars( $query_vars ){
    $query_vars[] = 'project';
    return $query_vars;

Then in your template, use get_query_var('project'); to retrieve the value of project.

Your Answer

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

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