0

I have 2 taxonomies type and city

I want to have page where it will dynamically query a city and type depending on the link for example:

type/condo/city/toronto should get all my properties from Toronto that is a condo.

I tried adding a taxonomy page where it would have a filename of taxonomy-type-city.php but it seems to not work.

Also tried to add a custom rewrite rule and query but still does not work. Is this possible?

    <?php
// code will goes here

function custom_rewrite_rule() {
    add_rewrite_rule('^property-type/([^/]*)/city/([^/]*)/?','?property_type=$matches[1]&city=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);


function custom_query( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
        if ( $query->is_tax( 'property_type' ) || $query->is_tax( 'city' ) ) {
            $query->set( 'tax_query', array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'property_type',
                    'field'    => 'slug',
                    'terms'    => get_query_var( 'property_type' ),
                ),
                array(
                    'taxonomy' => 'city',
                    'field'    => 'slug',
                    'terms'    => get_query_var( 'city' ),
                ),
            ) );
        }
    }
}
add_action( 'pre_get_posts', 'custom_query' );
?>
3
  • 1
    Tested the rewrite rule, and needed to add index.php to the second parameter to get it working: index.php?property_type....
    – Caleb
    Commented Jul 22, 2023 at 14:19
  • hmmm but I have my permalink to not have a index.php not sure why it's still not working Commented Jul 24, 2023 at 2:12
  • Internally WordPress will always use index.php.
    – Caleb
    Commented Jul 24, 2023 at 4:25

0

Your Answer

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