WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/

share|improve this question
what is a project? a custom post type? – Milo May 7 '12 at 17:56
no. it's nothing, just a query variable i'm using – Alexa May 7 '12 at 17:56
you can pass project as a query var, but your destination has to be something, like a page. – Milo May 7 '12 at 19:33
up vote -2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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