remove query arg from url after set query


i make migration plugin from spicific script to wordpress but i have a problem on redirect old urls to new one

old urls is like book.php?view=12a34b56
and i want redirect it to book/12a34b56

i try to make it by add_rewrite_rule like this

init function >

but rewrite rule not detect query args i don’t know why, so i’m changed the method to

query_vars function >
        array_push($vars, 'view');

pre_get_posts function >
        if (!is_admin() && is_main_query() && get_query_var('pagename') === 'book.php' && get_query_var('view') != '') {
            $query->set('name', get_query_var('view'));

            // after redirect to new url, view arg not removed
            // i try to remove view arg in url after set query but it's not removed
            // and new url be like this book/12a34b56/?view=12a34b56
            $query->set('view', false);
            set_query_var('view', false);

this method work perefectly but i still have one problem like i say in code comments
new url be like book/12a34b56/?view=12a34b56,
how i can remove ?view=12a34b56 after set post name, or method to detect query args on add_rewrite_rule that will be great.

NOTE : i do not want edit .htaccess directly

Boyzen 4 months 0 Answers 23 views 0

Leave an answer