0

I'm trying to add some rewrite rules for a member based directory that is filterable

So for instance if I had the following URLs

www.example.com/members/location=gb/

www.example.com/members/location=gb/role=director/

I would normally just do this as such

$member_page_id = 123;
add_rewrite_rule( 'members/search/location =([^/]+)/?$',
    'index.php?page_id=' . $member_page_id . '&location=$matches[1]',
    'top'
);
add_rewrite_rule( 'members/search/location =([^/]+)/role=([^/]+)/?$',
    'index.php?page_id=' . $member_page_id . '&location=$matches[1]&role=$matches[2]',
    'top'
);

But how can I rewrite these if I want to have multiple variables of the same or they are selected in a different order, say:

www.example.com/members/location=gb/location=us/role=director/

www.example.com/members/location=gb/role=director/location=us/

I can probably re-order the search string with parse query so it's always say location first then role so as not to have multiple URLs for the same page but how do I add a rewrite rule when there is an unknown amount of variables?

0

For those interested I got around it by doing the following

add_rewrite_rule( 'members/search/(.+?)?$',
    'index.php?page_id=' . $member_page_id . '&search=$matches[1]',
    'top'
);

So essentially getting everything after the search then dealing that when parsing the query I then parsed the query like so from the 'search' query_var

function wpse_set_pre_get_users( $query ){
    if( ! is_admin() ){

        $meta_query = $query->get( 'meta_query' );
        $meta_new_query = array();

        if( get_query_var( 'search' ) != '' ){
            $search_arr = explode( '/', get_query_var( 'search' ) );

            foreach ( $search_arr as $search_val ) {
                $vals = explode( '=', $search_val );

                $serialize_value = serialize( strval( urldecode( $vals[ 0 ] ) ) );
                $meta_new_query = array(
                    array(
                        'key' => $vals[ 1 ],
                        'value' => trim( urldecode( $vals[ 0 ] ) ),
                        'compare' => '=',
                    ), 
                    array(
                        'key' => $vals[ 1 ],
                        'value' => trim( urldecode( $vals[ 0 ] ) ),
                        'compare' => 'LIKE',
                    ), 
                    array(
                        'key' => $vals[ 1 ],
                        'value' => trim( $serialize_value ),
                        'compare' => 'LIKE',
                    ), 
                    'relation' => 'OR',
                );
                $meta_query = array_merge( $meta_query, array( $meta_new_query ) );
            }

            $query->set( 'meta_query', $meta_query );
        }
    }
}

add_action( 'pre_get_users','wpse_set_pre_get_users' );

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.