WP Property Drive/docs

Routing enquiry emails by property status

Some agencies want sales and lettings enquiries to go to different inboxes. There are two ways to do this. Use the filter unless you also need the stored agent email to change.

wppd_contact_email_recipient runs before the property enquiry form ([cf8]/[cf9]) and the viewing request form send their email. It changes only the recipient, does not touch stored data, and is not undone by imports.

Add this to a small custom plugin or the child theme’s functions.php:

add_filter(
    'wppd_contact_email_recipient',
    function ( $to, $property_id, $viewing ) {
        $status = (string) get_post_meta( $property_id, 'property_status', true );

        $sales    = [ 'For Sale', 'Sale Agreed', 'Sold' ];
        $lettings = [ 'To Let', 'Let', 'Let Agreed', 'Has Been Let', 'Has been Let' ];

        if ( in_array( $status, $sales, true ) ) {
            return 'sales@example.com';
        }

        if ( in_array( $status, $lettings, true ) ) {
            return 'lettings@example.com';
        }

        return $to;
    },
    10,
    3
);

Option 2: rewrite the stored agent email

Use this only if the property’s agent_email itself must change, for example because it is displayed or exported.

Importers rewrite agent_email every time they update an unlocked property. Run the update again after imports, or lock the properties.

Run it once with WP-CLI. Save the script outside the web root, for example as update-agent-emails.php:

<?php
$sales    = [ 'For Sale', 'Sale Agreed', 'Sold' ];
$lettings = [ 'To Let', 'Let', 'Let Agreed', 'Has Been Let', 'Has been Let' ];

$property_ids = get_posts(
    [
        'post_type'      => 'property',
        'post_status'    => 'any',
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'no_found_rows'  => true,
    ]
);

$updated = 0;

foreach ( $property_ids as $property_id ) {
    $status = (string) get_post_meta( $property_id, 'property_status', true );

    if ( in_array( $status, $sales, true ) ) {
        update_post_meta( $property_id, 'agent_email', 'sales@example.com' );
        $updated++;
    } elseif ( in_array( $status, $lettings, true ) ) {
        update_post_meta( $property_id, 'agent_email', 'lettings@example.com' );
        $updated++;
    }
}

WP_CLI::success( "Updated agent_email on {$updated} properties." );
wp eval-file update-agent-emails.php

Back up the database first. Do not trigger a bulk update like this from a public URL parameter: anyone who finds the URL could run it.