cac/addon/filtering/options

Deprecated This documentation is deprecated since Admin Columns version 4.
Please view the updated version of this documentation here.

Use the filter cac/addon/filtering/options to change the options of the filtering dropdown.

Usage

/**
 * Filtering: filter dropdown options
 *
 * @param array $options All the filtering options: value => label
 * @param CPAC_Column $column Column class instance
 */
function my_cac_filtering_options( $options, $column ) {

    // Possibly modify $options

    return $options;
}
add_filter( 'cac/addon/filtering/options', 'my_cac_filtering_options', 10, 2 );

Examples

Human readable dates

Change each date option into a human readable format.

/**
 * Filtering: Change each date option into a human readable format
 *
 * @param array $options All the filtering options: value => label
 * @param CPAC_Column $column Column class instance
 */
function my_cac_filtering_options_change_label_to_readable_date( $options, $column ) {
	$custom_field = 'date'; // enter your custom field key
	$column_type = 'column-meta';  // enter type of field. You can find this by hovering of the Type label of the column settings.
	if ( $column_type == $column->get_type() && $custom_field == $column->get_field() ) {
		foreach ( $options as $value => $label ) {

			// change the date format of the label to a readable format
			$options[ $value ] = date_i18n( 'j F Y', strtotime( $value ) );
		}
	}
	return $options;
}
add_filter( 'cac/addon/filtering/options', 'my_cac_filtering_options_change_label_to_readable_date', 10, 2 );

Sorted by user’s last name

Sort all user-custom-field options by user’s last name

/**
 * Filtering: Sort all custom field options by user's last name
 *
 * Filter cac/addon/filtering/options
 *
 * @param array $options Dropdown Options, it's value and labels
 * @param object $column CPAC_Column object.
 * @return array Options
 */
function my_cac_filtering_options_sort_by_user_last_name( $options, $column ) {
    $custom_field = 'my-custom-field'; // enter your field
    $column_type = 'meta'; // type of field: custom field. You can find this by hovering of the Type label of the column settings.
    if ( $column->is_type( $column_type ) && $column->is_field( $custom_field ) ) {
        $unsorted = array();
        foreach ( $options as $id => $label ) {
            if ( $userdata = get_userdata( $id ) ) {
                $unsorted[ $id ] = $userdata->last_name;
            }
        }
        asort( $unsorted ); // sort by lastname
        $sorted = array();
        foreach ( $unsorted as $id => $label ) {
            $sorted[ $id ] = $options[ $id ];
        }
        $options = $sorted;
    }
    return $options;
}
add_filter( 'cac/addon/filtering/options', 'my_cac_filtering_options_sort_by_user_last_name', 10, 2 );

Add post ID to option label

Add the post ID to the options label of the custom field column dropdown (type: Post Title).

/**
 * Filtering: Change option label to include post ID
 *
 */
function my_cac_filtering_options_add_post_id( $options, $column ) {
	$column_type = 'column-meta';  // enter type of field. You can find this by hovering of the Type label of the column settings.
	$custom_field_type = 'title_by_id'; // enter type of custom field. You can find this by hovering of the Type label of the custom "Field Type" settings.
	if ( $column_type == $column->get_type() && $custom_field_type == $column->get_field_type() ) {
		foreach ( $options as $key => $label ) {

			// change the date format of the label to a readable format
			$options[ $key ] = $label . ' (' . $key . ')';
		}
	}
	return $options;
}
add_filter( 'cac/addon/filtering/options', 'my_cac_filtering_options_add_post_id', 10, 2 );

 Change label for ACF column

In this example we added a text to the option label for a ACF column.

/**
 * Filtering: Change option label
 *
 */
function my_cac_filtering_options_change_acf_label( $options, $column ) {
	$column_type = 'column-acf_field';  // enter type of field. You can find this by hovering of the Type label of the column settings.
	$field_label = 'Number'; // enter label of the ACF field.
	//if ( $column_type == $column->get_type() && $acf_field_type == $column->get_field_type() ) {
	if ( $column_type == $column->get_type() ) {
		$field = $column->get_field();
		if ( $field_label == $field['label'] ) {
			foreach ( $options as $key => $label ) {
				// change label
				$options[ $key ] = $label . ' (add something to label)';
			}
		}
	}
	return $options;
}
add_filter( 'cac/addon/filtering/options', 'my_cac_filtering_options_change_acf_label', 10, 2 );