Last active
October 9, 2024 14:46
-
-
Save mklasen/beb56fa943290e318e5cf87f142b61eb to your computer and use it in GitHub Desktop.
Add custom field and field options to Search Filters WordPress Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Search_Filter\Fields\Settings as Fields_Settings; | |
class SearchFilters { | |
public function __construct() { | |
$this->init(); | |
} | |
public function init() { | |
$this->hooks(); | |
} | |
public function hooks() { | |
add_action( 'plugins_loaded', array($this, 'plugins_loaded') ); | |
} | |
public function plugins_loaded() { | |
add_filter( 'search-filter/field/choice/options', array($this, 'add_su_status_field_options'), 10, 2 ); | |
add_filter( 'search-filter/queries/query/apply_wp_query_args', array($this, 'filter_results'), 10, 1 ); | |
$this->add_status_field_option_to_data_type(); | |
} | |
public function add_su_status_field_options( $options, $field ) { | |
$data_type = $field->get_attribute( 'dataType' ); | |
if ($data_type === 'su_status') { | |
return array( | |
array( | |
'value' => 'in-progress', | |
'label' => 'In Progress', | |
), | |
array( | |
'value' => 'not-started', | |
'label' => 'Not started', | |
) | |
); | |
} | |
return $options; | |
} | |
public function filter_results( $query_args ) { | |
$course_ids = learndash_user_get_enrolled_courses( get_current_user_id() ); | |
if (isset( $_GET['_field_15'] ) && $_GET['_field_15'] === 'in-progress') { | |
$query_args['post__in'] = $course_ids; | |
} | |
return $query_args; | |
} | |
public function add_status_field_option_to_data_type() { | |
$data_type_setting = Fields_Settings::get_setting( 'dataType' ); | |
if (!$data_type_setting) { | |
return; | |
} | |
$acf_data_type_option = array( | |
'label' => __( 'SU Status', 'search-filter' ), | |
'value' => 'su_status', | |
); | |
$data_type_setting->add_option( $acf_data_type_option ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment