<?php
/** Add fake facet choices as ghosts - for example if you need to display terms that
* don't have any posts in them yet, and you want them to appear as ghost choices.
* Note: allowing users to make non-existant selections can have weird side-effects.
*/
/**
* This example is for basic non-hierarchal Checkboxes facet, but
* could be adapted for Radio facets.
*/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'my_checkboxes_facet' == $params['facet']['name'] ) { // Adjust the name of the facet
/** Array of facet_value (the technical value of the choice), facetwp_display_value (the display value of the choice).
* Could be added manually as below, or with some custom code
* to find the values that you need from terms or other sources.
*/
$fake_facets = [ 'fake1' => 'Fake 1', 'fake2' => 'Another Fake' ];
$fake_output = '';
foreach ( $fake_facets as $fake => $fake_display ) {
$fake_output .= '<div class="facetwp-checkbox disabled" data-value="' . $fake . '">' . $fake_display . ' <span class="facetwp-counter">(0)</span></div>';
}
$output = $output . $fake_output;
}
return $output;
}, 10, 2);
/**
* This example is for basic non-hierarchal Dropdown facet
*/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'my_dropdown_facet' == $params['facet']['name'] ) { // Adjust the name of the facet
// Remove the <select> tags and capture the attributes
$pattern = '/(<select.*?>)(.*)(<\/select>$)/s';
$opening_tag = '';
$closing_tag = '<\/select>';
$options_content = '';
if (preg_match($pattern, $output, $matches)) {
$opening_tag = $matches[1];
$options_content = $matches[2];
$closing_tag = $matches[3];
}
// Array of facet_value (the technical value of the choice), facetwp_display_value (the display value of the choice).
$fake_facets = [ 'fake1' => 'Fake 1', 'fake2' => 'Another Fake' ];
$fake_output = '';
foreach ( $fake_facets as $fake => $fake_display ) {
$fake_output .= '<option value="' . $fake . '" disabled>' . $fake_display . '</option>'; // fake choices need the 'disabled' attribute
}
// Reconstruct the HTML, determines where the face choices are added
$output = $opening_tag . $fake_output . $options_content . $closing_tag;
}
return $output;
}, 10, 2);