Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Daniel

Modify sorting of meta column value

Hey guys,

i wonder if it’s possible to change the sorting behaviour of columns whose column values are modified (for display).

i use this code to change the values:


// sum up MEP voting columns
add_filter( 'cac/column/meta/value', function ( $value, $object_id, $column ) {

$cf_keys = array('up_votings', 'down_votings');

if ( 'column-meta' == $column->get_type() AND in_array($column->get_field(), $cf_keys) ) {
$value = array_reduce(explode(',', $value), function($item, $carry) {
$carry += intval($item);
return $carry;
}, '');
}

return $value;

}, 10, 3 );

basically, I sum up the values that come from a serialized array (i.e. a:3:{i:16861;i:1;i:16859;i:1;i:16892;i:1;}) but this obviously troubles the sorting mechanics.

i already tried different field types like ‘numeric’, ‘multiple values’ and ‘count’ but all of them obviously can’t deal with it.

do we need to add a custom column type for this then?

7 years, 9 months ago
Daniel

ok, i used a custom column instead, there i can do whatever i need in the class method

get_raw_value()
7 years, 9 months ago
Daniel

yes, that’s right. i used the addon-framework as a start.
but, on digging further in the source of your plugin, i found an undocumented filter in classes/column/custom-field.php: ‘cac/column/meta/raw_value’

	/**
	 * @see CPAC_Column::get_raw_value()
	 * @since 2.0.3
	 */
	public function get_raw_value( $id, $single = true ) {
		$raw_value = '';

		if ( $field_key = $this->get_field_key() ) {
			$raw_value = get_metadata( $this->get_meta_type(), $id, $field_key, $single );
		}

		return apply_filters( 'cac/column/meta/raw_value', $raw_value, $id, $field_key, $this );
	}

i then used this to overwrite the raw value of my fields to fix the sorting as well:

add_filter( 'cac/column/meta/raw_value', function ( $raw_value, $object_id, $field_key, $column ) {

	$cf_keys = array('up_votings', 'down_votings');

	if ( 'column-meta' == $column->get_type() AND in_array($column->get_field(), $cf_keys) ) {
		$raw_value = array_reduce($raw_value, function($item, $carry) {
			$carry += intval($item);
			return $carry;
		}, '');
	}

	return $raw_value;

}, 10, 4 );

so actually, no need for a custom plugin/addon, but both ways work.

7 years, 9 months ago
Daniel

just one more addition, to also make it work if the sorting is not active, i added another check on the data type returned by the filter:

// use raw_value filter instead to make the sorting work
add_filter( 'cac/column/meta/raw_value', function ( $raw_value, $object_id, $field_key, $column ) {

	$cf_keys = array('up_votings', 'down_votings');
	error_log(var_export($raw_value, 1), 0);
	if ( 'column-meta' == $column->get_type() AND in_array($column->get_field(), $cf_keys) ) {

		if( !is_array($raw_value) ) {
			$raw_value = explode(',', $raw_value);
		}

		$raw_value = array_reduce($raw_value, function($item, $carry) {
			$carry += intval($item);
			return $carry;
		}, '');
	}

	return $raw_value;

}, 10, 4 );

this way i only need to hook into the cac/column/meta/raw_value filter.

7 years, 9 months ago
Daniel

oh, and you should definitely update your documentation on this one. =)

7 years, 9 months ago
Stefan van den Dungen Gronovius
Developer

Hi Daniel,

Thanks for noticing.
We will update the documentation for this.

7 years, 9 months ago

You must be logged in to reply to this topic.