http://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable–wp-25095 would be a good tut on this. The columns are all generated by WP in this case – there is no custom tables or anything in Job Manager.
Hi Mike,
Thanks for this informative and helpful resource. I’ve figured out most of my puzzles. Basically, for the standard fields in job manager, I used $query->set(‘orderby’,’title’); to filter those fields. It is successful.
However, for “_application_deadline” post meta, the little sorting triangle appears, but the sorting does not work as expected. The code snippet below:
if ( __( ‘Closing Date’, ‘job_manager_app_deadline’ ) == $orderby ) {
$query->set(‘meta_key’,’_application_deadline’);
$query->set(‘orderby’,’meta_value’);
}
Could you help me further regarding this? Thanks a lot!!!
Try meta_value_num – I think its a timestamp
Hi Mike,
“meta_value_num” still seems do not work.
“_application_deadline” meta field comes from “Application Deadline Plugin” and it is new added field. The code for its addition is below:
<em> public function columns( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $value ) {
if ( $key == 'job_expires' )
$new_columns['job_deadline'] = __( 'Closing Date', 'job_manager_app_deadline' );
$new_columns[ $key ] = $value;
}
return $new_columns;
}
public function custom_columns( $column ) {
global $post;
if ( $column == 'job_deadline' ) {
if ( ! ( $deadline = get_post_meta( $post->ID, '_application_deadline', true ) ) )
echo '<span class="na">–</span>';
else
echo date_i18n( __( 'M j, Y', 'job_manager' ), strtotime( $deadline ) );
}
}
</em>
Thanks.
Wei
What about your sorting code. Can you paste that too.
No Problem, Mike. Here it is:
add_filter( 'manage_edit-job_listing_sortable_columns', 'my_sortable_job_listing_column' );
function my_sortable_job_listing_column( $columns ) {
$columns["job_position"] = __( "Position", 'wp-job-manager' );
$columns["job_posted"] = __( "Posted", 'wp-job-manager' );
$columns["job_expires"] = __( "Expires", 'wp-job-manager' );
$columns['job_deadline'] = __( 'Closing Date', 'job_manager_app_deadline' );
//To make a column 'un-sortable' remove it from the array
//unset($columns['date']);
return $columns;
}
add_action( 'pre_get_posts', 'my_job_listing_orderby' );
function my_job_listing_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( __( "Position", 'wp-job-manager' ) == $orderby ) {
$query->set('orderby','title');
}
if ( __( "Posted", 'wp-job-manager' ) == $orderby ) {
$query->set('orderby','date');
}
if ( __( "Expires", 'wp-job-manager' ) == $orderby ) {
$query->set('meta_key','_job_expires');
$query->set('orderby','meta_value_num');
}
if ( __( 'Closing Date', 'job_manager_app_deadline' ) == $orderby ) {
$query->set('meta_key','_application_deadline');
$query->set('orderby','meta_value_num');
}
}
You should be comparing the key with == $orderby, not the label. e.g. job_deadline
I’ve tried to use the key for all comparisons at first sight. It does not work. Then I change to use label afterwards, others are working fine instead of job_deadline. 😐 very weird behavior
Not sure why, but if ( ‘ClosingDate’ == $orderby ) { worked for me.