I am trying to get from this kind of url:
domain.com/page/classes/?language=english&need=pro
To this:
domain.com/class/english-pro
MORE INFORMATIONS
Both variables have been added as query_vars
. classes
is the current page and page
is the parent page.
WHAT I'VE TRIED
Here is the rewrite rule I've tried but it's not working.
function custom_rewrite() {
add_rewrite_rule( 'class/([^/]+)-([^/]+)$', 'index.php?language=$matches[1]&need=$matches[2]', 'top' );
}
add_action( 'init', 'custom_rewrite' );
For some reasons I can't get my head around the regex expressions... Thanks for your help!
UPDATE 1
With a bit of searching this what I came up with:
function custom_rewrite() {
add_rewrite_tag( '%_language%', '([a-zA-Z\d\-_+]+)' );
add_rewrite_tag( '%_need%', '([a-zA-Z\d\-_+]+)' );
add_rewrite_rule( 'class/([a-zA-Z\d\-_+]+)-([a-zA-Z\d\-_+]+)?', 'index.php?_language=$matches[1]&_need=$matches[2]', 'top' );
}
add_action( 'init', 'custom_rewrite' );
But it's still not working.
UPDATE 2
I've made some changes so that it gets easier to do the redirect. Here is how it works:
- I've got two selects in a form whith which people can choose a
language
and aneed
. Both are declared query variables. - When the form validates it loads this url:
domain.com/page/classes/?_language=english&_need=pro
. - The result's page loads informations from a custom post type named with the following structure language-need. I get the informations by using a
query_posts
that build thename
from thequery variables
.
Now what I want is simple : to redirect the defaut url (domain.com/page/classes/?language=english&need=pro
) to this new url (domain.com/class/english-pro
) while still loading the same informations**.
UPDATE 3
I've tried @jgraup solution below (without the prefix__pre_post_link
function as I've simplified my url to not need it) but it's not working (my custom post type is using the same settings
if ( ! class_exists( 'PrefixClassesRewrites' ) ) {
class PrefixClassesRewrites {
const POST_TYPE_SLUG = 'lang';
function __invoke() {
add_action( 'init', array ( $this, 'prefix__init' ) );
add_action( 'pre_get_posts', array ( $this, 'prefix__pre_get_posts' ) );
}
public function prefix__init() {
// custom query params that we check for later
add_rewrite_tag( '%_language%', '([a-zA-Z\d\-_+]+)' );
add_rewrite_tag( '%_need%', '([a-zA-Z\d\-_+]+)' );
add_rewrite_tag( '%_page_class%', '_page_class' );
// rewrite rule to transform the URL into params
add_rewrite_rule( 'class/([a-zA-Z\d\-_+]+)-([a-zA-Z\d\-_+]+)?', 'index.php?_language=$matches[1]&_need=$matches[2]&_page_class=1', 'top' );
flush_rewrite_rules(); // <-- for testing only | removed once the rewrite rules are in place
}
public function prefix__pre_get_posts( $query ) {
if ( isset( $query->query_vars[ '_page_class' ] ) ) {
// Convert the variables to a custom post type name.
$name = implode( '-', array (
$query->query_vars[ '_langue' ],
$query->query_vars[ '_besoin' ],
) );
// Add the name and post_type to the query.
$query->query_vars[ 'name' ] = $name;
$query->query_vars[ 'post_type' ] = array ( static::POST_TYPE_SLUG );
// Nothing else to do here.
return;
}
}
}
$prefixClassesRewrites = new PrefixClassesRewrites();
$prefixClassesRewrites(); // kick off the process
}
The thing is that nothing has changed, my url is not being rewritten. By the way, is the rewriting tule supposed to be in y htaccess when it's working?
add_rewrite_rule
to make your custom query args come through. See codex.wordpress.org/Rewrite_API/add_rewrite_rule But it's just simply specifying thatlanguage
is a thing. If it messes up your query then I found_language
doesn't. Just don't expect to use a $_GET for these rewrites. – jgraup Nov 30 '16 at 19:02/domain.com/page/class-5-6
? Wherelanguage=5
andneed=6
– jgraup Nov 30 '16 at 19:05/domain.com/page/class-post_name-term_slug
wherelanguage=5
(5 being thepost_id
) andneed=6
(6 being theterm_id
). – Pipo Nov 30 '16 at 19:07