Generating regions for existing listings

I have regions saved with listings but had misconfiguration for mapbox and regions were not created. Is there a way to create regions from existing listings and link them with these listings?

Hi,

There is no built-in feature, but you can try the following solution.

This script should generate regions for 100 listings per request, once you see the “regions generated for…” you can refresh the page. Please note that it’s better to remove the existing regions first, I recommend creating a backup before this. After the first run, you can check the regions generated for the first 100 listings and if the listings are searchable. Please remove the script once regions are generated.

add_action(
    'template_redirect',
    function () {
        if ( ! isset( $_GET['generate_regions'] ) ) {
            return;
        }

        $ids = get_posts(
            [
                'status'         => 'publish',
                'post_type'      => 'hp_listing',
                'posts_per_page' => 100,
                'fields'         => 'ids',
                'orderby'        => 'rand',
            ]
        );

        $count = 0;

        foreach ( $ids as $id ) {
            if ( ! wp_get_post_terms( $id, 'hp_listing_region', [ 'fields' => 'ids' ] ) ) {
                do_action( 'hivepress/v1/models/listing/update_longitude', $id );

                $count++;

                usleep( 100000 );
            }
        }

        echo 'regions generated for ' . $count . ' listings';

        die();
    }
);

Just one more thing to note, please make sure you don’t exceed the free-tier limit if you’re on the free plan, because this script makes an API call for each listing to generate the regions, which can add up quickly.

Hope this helps