I'm using two custom fields in wordpress to filter a persons database (custom taxonomy = persons) by gender and age. I would like the domain path name to look like that:
domain/persons/women/40years/
My current code in functions.php
is working, but only, if I use a single parameter name "women40years", which results in this domain path name:
domain/persons/women40years/
function join_test( $join ) {
if ( preg_match( '#women40years#', $_SERVER['REQUEST_URI'] ) ) {
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta as meta_1 ON ( $wpdb->posts.ID = meta_1.post_id ) LEFT JOIN $wpdb->postmeta as meta_2 ON ( $wpdb->posts.ID = meta_2.post_id )";
}
return $join;
}
add_filter( 'posts_join' , 'join_test' );
function where_test( $where, $query ) {
if ( preg_match( '#women40years#', $_SERVER['REQUEST_URI'] ) ) {
$where .= " AND meta_1.meta_key = 'gender' AND meta_1.meta_value = 'women' AND meta_2.meta_key = 'age' AND meta_2.meta_value = 40";
}
return $where;
}
add_filter( 'posts_where', 'where_test', 10, 2 );
add_rewrite_rule( 'persons/women40years/?$', 'index.php?thema=persons&g=women40years', 'top' );
I guess the solution would have to do with GET
parameters, pretty URL rewrite or similar, but I don't know how to handle my custom field values for that. Thank you very much in advance for your help!
I'm using two custom fields in wordpress to filter a persons database (custom taxonomy = persons) by gender and age. I would like the domain path name to look like that:
domain.com/persons/women/40years/
My current code in functions.php
is working, but only, if I use a single parameter name "women40years", which results in this domain path name:
domain.com/persons/women40years/
function join_test( $join ) {
if ( preg_match( '#women40years#', $_SERVER['REQUEST_URI'] ) ) {
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta as meta_1 ON ( $wpdb->posts.ID = meta_1.post_id ) LEFT JOIN $wpdb->postmeta as meta_2 ON ( $wpdb->posts.ID = meta_2.post_id )";
}
return $join;
}
add_filter( 'posts_join' , 'join_test' );
function where_test( $where, $query ) {
if ( preg_match( '#women40years#', $_SERVER['REQUEST_URI'] ) ) {
$where .= " AND meta_1.meta_key = 'gender' AND meta_1.meta_value = 'women' AND meta_2.meta_key = 'age' AND meta_2.meta_value = 40";
}
return $where;
}
add_filter( 'posts_where', 'where_test', 10, 2 );
add_rewrite_rule( 'persons/women40years/?$', 'index.php?thema=persons&g=women40years', 'top' );
I guess the solution would have to do with GET
parameters, pretty URL rewrite or similar, but I don't know how to handle my custom field values for that. Thank you very much in advance for your help!
I solved the problem with this code:
function add_query_vars_filter( $vars ){
$vars[] = "years";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
add_rewrite_rule('^persons/(women|men)/([0-9]{2})years/?$', 'index.php?thema=persons&g=$matches[1]&years=$matches[2]', 'top');
thema
query variable used in the rewrite rule destination? – bosco Commented Nov 12, 2024 at 19:07