Add ‘Include Sold STC’ checkbox to search forms

Step 1 - Adjust query

We need to adjust the main property query to exclude any non-available properties by default.

This can be done by adding the following to your themes functions.php or using a plugin like Code Snippets:

add_action( 'pre_get_posts', 'remove_sold_stc_by_default' );
function remove_sold_stc_by_default( $q ) 
{
    if (is_admin())
        return;

    if (!$q->is_post_type_archive('property') && !$q->is_tax(get_object_taxonomies('property')))
        return;

    if (isset($_GET['shortlisted']))
        return;

    $tax_query = $q->get('tax_query');

    if ( !isset($_REQUEST['include_sold_stc']) ) 
    {
        if (!is_array($tax_query)) { $tax_query = array(); }

        // NOTE: change (10, 14) to the IDS of 'For Sale' and 'To Let'
        // These can be found under 'Property Hive > Setting > Custom Fields'
        $tax_query[] = array(
            'taxonomy' => 'availability',
            'field' => 'term_id',
            'terms' => array(10, 14), 
            'operator' => 'IN'
        );
    }

    $q->set('tax_query', $tax_query);
}

add_filter( 'propertyhive_search_form_fields_after', 'remove_sold_stc_hidden', 10, 1 );
function remove_sold_stc_hidden( $form_controls )
{
    if ( isset($form_controls['include_sold_stc']) ) 
    {
        unset($form_controls['include_sold_stc']);
    }
    return $form_controls;
}

Step 2 - Add checkbox to search form template

Next we need to add the 'Include Sold STC' checkbox to our search form. We can do this by overriding our search form template and adding the following where we want our checkbox to appear:

// Copy: /wp-content/plugins/propertyhive/templates/global/search-form.php
// To: /wp-content/themes/{your-theme-name}/propertyhive/global/search-form.php
// Include the following where you want the checkbox to appear inside the search-form.php:

<div class="control control-include-sold-stc"><label><input type="checkbox" value="1" name="include_sold_stc"<?php if (isset($_REQUEST['include_sold_stc'])) { echo ' checked'; } ?>> Include Under Offer, Sold STC</label></div>