0

I have a page named aircraft-gallery and I am showing the data in this page from database its working fine.

I have another page named aircraft-gallery-class i am sending the data in ?id variable and receiving that data using GET['id'] than I sending this data to the database for results it is also working fine.

Here is my controller in WordPress function to get id

public function view_air_crafts_by_class(){
        //var_dump(get_query_var('id')); //working fine
        //die();
        return $this->air_craft_model->get_air_craft_by_class($_GET['id']);
    }

Now my question is simple I have this URL

http://example.com/aircraft-gallery/aircraft-gallery-class/?id=VIP Airliner

and I want this URL

http://example.com/aircraft-gallery/VIP-Airliner

I am using these lines of code in my functions.php file to convert the URL but it's not working, I don't know why

add_filter( 'query_vars', 'wpa5413_query_vars' );
function wpa5413_query_vars( $query_vars )
{
    $query_vars[] = 'id';
    return $query_vars;
}

add_action( 'init', 'wpa5413_init' );
function wpa5413_init()
{
    add_rewrite_rule(
        'aircraft-gallery-class/([^/]+)/?',
        'index.php?pagename=aircraft-gallery-class&id=$matches[1]',
        'top' );
}

I have already tried many codes suggested by the community but it does not work.

Any help would be greatly appreciated. I am clicking saving permalinks every time I update the functions.php file. Thanks

1
  • You have the right idea, although id is a very generic variable to use – Tom J Nowell Sep 5 '17 at 16:01
0

The problem is that id is no longer a $_GET variable, and is instead a WP query variable, so use the get_query_var function instead

18
  • I am new in WordPress that's why I am facing problem how can I use get_query_var could you please help me. – Komal khan Sep 5 '17 at 16:05
  • echo $_GET['id'] echo get_query_var( 'id' ) – Tom J Nowell Sep 5 '17 at 16:06
  • could you please solve my issue where I can put this get_query_var...? – Komal khan Sep 5 '17 at 16:11
  • @Komalkhan without seeing the code for your page it's not possible to say, but anywhere you see $_GET['id'] or $_REQUEST['id'] needs changing to get_query_var('id') – Tom J Nowell Sep 5 '17 at 21:39
  • here is my function in my controller public function view_air_crafts_by_class(){ //var_dump($one); //echo get_query_var('id'); //die(); return $this->air_craft_model->get_air_craft_by_class(get_query_var('id')); } – Komal khan Sep 6 '17 at 9:36

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.