1

domain.com/rents/miami-dade/

domain.com/companies/miami-dade/

Then for, say rents/miami-dade/ I would loop through some different areas within miami, showing a list of rents for each area. For example:

domain.com/rents/miami-dade/

would render something like this by looping through the areas in Miami:

North Miami Beach

  • rents table

Hialeah

  • rents table

Cutler Ridge

  • rents table

I'd need to mimic the exact same sequence for a different object, 'companies'.

domain.com/companies/miami-dade/

would render something like this by looping through the areas in Miami:

North Miami Beach

  • companies table

Hialeah

  • companies table

Cutler Ridge

  • companies table

How could I go about setting something like this up? I've already created a companies post type, and made taxonomies for counties and cities.

I'm already able to see a taxomonie's own page, but it's not limited to a certain post type, which isn't what I need. I need to url to resemble something like what I showed here.

Thanks in advance!

2
2

You can accomplish this with add_rewrite_rule().

I personally like to show this example in a class to make it copy/paste ready. You could throw this in a plugin or functions.php -- some place that loads this code before query_vars, parse_request and init.

The goal is to add rewrite rules, make sure you can add custom properties to the main query, then replace the default template to be loaded with your custom template.

if (!class_exists('RentsAndCompaniesRewrite')):

    class RentsAndCompaniesRewrite
    {
        // WordPress hooks

        public function init()
        {
            add_filter('query_vars', array($this, 'add_query_vars'), 0);
            add_action('parse_request', array($this, 'sniff_requests'), 0);
            add_action('init', array($this, 'add_endpoint'), 0);
        }

        // Add public query vars

        public function add_query_vars($vars)
        {
            $vars[] = 'show_companies';
            $vars[] = 'show_rents';
            $vars[] = 'location';

            return $vars;
        }

        // Add API Endpoint

        public function add_endpoint()
        {
            add_rewrite_rule('^rents/([^/]*)/?', 'index.php?show_rents=1&location=$matches[1]', 'top');
            add_rewrite_rule('^companies/([^/]*)/?', 'index.php?show_companies=1&location=$matches[1]', 'top');

            flush_rewrite_rules(false); //// <---------- REMOVE THIS WHEN DONE TESTING
        }

        // Sniff Requests

        public function sniff_requests($wp_query) {
            global $wp;

            if ( isset( $wp->query_vars[ 'show_rents' ], $wp->query_vars[ 'location' ] ) ) {

                $this->handle_request_show_rents();

            } else if ( isset( $wp->query_vars[ 'show_companies' ], $wp->query_vars[ 'location' ] ) ) {

                $this->handle_request_show_companies();
            }
        }

        // Handle Requests

        protected function handle_request_show_rents()
        {
            add_filter('template_include', function ($original_template) {

                return get_stylesheet_directory() . '/templates/rents_by_location.php';
            });
        }

        protected function handle_request_show_companies()
        {
            add_filter('template_include', function ($original_template) {

                return get_stylesheet_directory() . '/templates/companies_by_location.php';
            });
        }
    }

    $wptept = new RentsAndCompaniesRewrite();
    $wptept->init();

endif; // RentsAndCompaniesRewrite

In a directory within your theme, add the templates you will use when the request is handled.

/templates/companies_by_location.php

<?php

global $wp;
$location = $wp->query_vars[ 'location' ];

echo 'Companies in the ' . $location . ' area:';

/templates/rents_by_location.php

<?php

global $wp;
$location = $wp->query_vars[ 'location' ];

echo 'Rents in the ' . $location . ' area:';
1

Ok, i have some basic ideas. You can install a plugin called Redirection. And after that, you can make like and redirect to some other pages with you own data.

but URL will change as the person redirect to the other specific page.

Now if you want to them to land on the specific page... you need to make the folder into server directory under the main site and inside it file containing index.php or index.html. Example: Goto you Cpanel and goto file maneger>> created the folder name if you want like, for "domain.com/companies/miami-dade/" create folder "companies" then subfolder "miami-dude". Note each folder should contain index.html file or it will not work. Note: this trick won't copy you theme else u need to make an entire HTML page and save it as index.html

Hope it might help you :-)

1
  • jgraup's answer was what I needed. Thanks for the help though! The rewrite rules are awesome!!!! – ninja08 Jun 12 '16 at 21:30

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.