Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the community forum?
All Topics
Karen

Sorting in custom column not working fine

Hi,

I created a custom column to show the last modified date of a post,

My get_raw_value function looks like this:
public function get_raw_value( $post_id ) {
$m_orig = get_post_field( 'post_modified', $post_id, 'raw' );
$m_stamp = strtotime( $m_orig );
$value = date('n/j/y @ g:i a', $m_stamp );
return $value;
}

I add the column in settings and I enabled the sort, however in All Post the column is not sorting correctly. I have data from 2015 and 2011, of course if I sort ASC the 2011 need to show up at the top and same when order DESC, 2015 should go to the top, but it brings me 2014 first.

Please advice.

4 years, 9 months ago
Stefan
Developer

Hi Karen,

We are happy to see that you want to create your own columns for our product.
Sorting is done on the raw value of a column. In your example you use the function get_raw_value(). Notice that there is also a get_value() function that can be used to format your data. So in your case you want the following results:

– get_raw_value() -> show date as a timestamp or yyyymmdd format
– get_value() -> show date as the desired format

the code should be something like:

public function get_raw_value( $post_id ) {
$p = get_post( $post_id );
return $p->post_modified;
}

public function get_value( $post_id ) {
$modified = $this->get_raw_value( $post_id );
return date( (‘n/j/y @ g:i a’, strtotime( $modified ) );
}

Please also be aware that there is already a column called ‘Last modified’ for the exact purpose you describe.

4 years, 9 months ago

You must be logged in to reply to this topic.