Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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_rewrite_rule('^news/([^/]*)/?','index.php?pagename=news&news_id=$matches[1]','top');    
}
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

1 Answer 1

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

 
discard

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.