url rewriting - Filtering output by two custom fields using posts_where and add_rewrite_rule

admin2025-01-08  5

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!

Share Improve this question edited Nov 12, 2024 at 16:28 Caleb 2,4441 gold badge14 silver badges15 bronze badges asked Nov 12, 2024 at 16:08 Andreas BöttcherAndreas Böttcher 112 bronze badges 2
  • What is the thema query variable used in the rewrite rule destination? – bosco Commented Nov 12, 2024 at 19:07
  • That's the name of the custom taxonomy. – Andreas Böttcher Commented Nov 12, 2024 at 21:22
Add a comment  | 

1 Answer 1

Reset to default 0

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'); 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270713a1449.html

最新回复(0)