I'm trying to make a custom rewrite rule the url is my-domain.com/web-directory/?loc=/directory/listing/john-smith and I want to rewrite to my-domain.com/web-directory/directory/listing/john-smith.

I need $_GET['loc']='/directory/listing/john-smith';

made a try with this function but did not work

function custom_query_vars( $query_vars ){
    $query_vars[] = 'loc';
    return $query_vars;
add_filter( 'query_vars', 'custom_query_vars' );

function custom_setup_rewrites(){
add_action( 'init', 'custom_setup_rewrites' );

Any help please !

  • Note that WordPress query vars don't end up in $_GET. If your code is expecting the value in $_GET, you will need to set it yourself via get_query_var. – Milo Jul 19 '17 at 3:17
  • Yes I use $loc=get_query_var( 'loc'); and the output for $loc that I receive is just "directory" – stav Jul 19 '17 at 9:42
  • Forgot to flush the rewrite rules :) after that the code working fine! – stav Jul 19 '17 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.