WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I need to rewrte this URL http://example.com/news/?news_id=2 to http://example.com/news/2 where news is the page and 2 is the news_id

I have added following code in functions.php:

function add_news_rewrite_rule() {
    add_rewrite_tag("%news_id%", '([^/]*)');
add_action( 'init', 'add_news_rewrite_rule' );

function my_insert_query_vars( $vars ){
    array_push($vars, 'news_id');
    return $vars;
add_filter( 'query_vars','my_insert_query_vars' ); 

And I am using $wp_query->query_vars['news_id'] instead of $_GET['news_id'] to pull out the data from database.

But this is not working. Please help me.

share|improve this question
Use get_query_var() instead of directly accessing $wp_query->query_vars['news_id'] – kaiser Mar 18 '13 at 21:23

Have a look at the Permalink settings in the admin, you should be able to achieve what you want there. Here is a link to the Permalink Codex

something like /%post_id% should do the job, if you select the 'Custom Structure' option.

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.