I have a project that has content being filtered throughout the site (many custom WP_QUERY calls). Originally there was no URL needs, just filtering by $_SESSION variable. Works perfectly, but NOW client wants each specialty to be loaded with a custom URL (much like categories do already). I need to to first know:

Is it possible to make a permalink structure with the $_SESSION variable?

If so:

How can I create a URL structure like this?

Archive Page:


Single Page:


1 Answer 1


So I was able to figure this out myself with the help of a great friend and php developer. I apologize some of the functionality i.e. CUSTOM::slugify() is custom but if necessary you can review my Div Starter theme to learn more about it.

add_action( 'init', 'cc_rewrite_add_rewrites' );
function cc_rewrite_add_rewrites() {
    $specialties = get_specialties(); //Fetches an array of postmeta defined from an ACF repeater field

    add_rewrite_rule( "^(" . implode("|", array_map(function($a) { return strtolower(CUSTOM::slugify($a)); }, $specialties) ) . ")/?", 'index.php', 'top' );

    // check path for specialty loop through specialties
    foreach($specialties as $specialty) {
        $slug = CUSTOM::slugify($specialty); // I needed to convert to a slug, custom function in Div Starter

        if (preg_match("/\/" . strtolower($slug) ."(\/|$)/i", strtolower($_SERVER['REQUEST_URI']) )) {
        // set session variable
        $_SESSION['specialty'] = $specialty;


I noticed my pagination was using this new permalink structure and breaking it so I was able to hook into it and remove the addition like this:

add_filter( 'get_pagenum_link', 'wpse_cc_pagenum_link' );

function wpse_cc_pagenum_link( $link ){
    return $url = str_replace( '/'.CUSTOM::slugify($_SESSION['specialty']), '', $link ); 

For more information about customizing your permalinks this guide was really helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.