• Resolved davidrevo

    (@davidrevo)


    Hi there,

    I’m just wondering how to add column sorting features on admin page of job manager. I’ve tried some code in function.php and added a filter to make the columns sortable. Unfortunately, it doesn’t work. Can you give some hints about how to do it?

    Thanks in advance,
    Wei

    https://www.ads-software.com/plugins/wp-job-manager/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Mike Jolley

    (@mikejolley)

    https://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.

    Thread Starter davidrevo

    (@davidrevo)

    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!!!

    Plugin Author Mike Jolley

    (@mikejolley)

    Try meta_value_num – I think its a timestamp

    Thread Starter davidrevo

    (@davidrevo)

    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">&ndash;</span>';
    			else
    				echo date_i18n( __( 'M j, Y', 'job_manager' ), strtotime( $deadline ) );
    		}
    	}
    </em>

    Thanks.
    Wei

    Plugin Author Mike Jolley

    (@mikejolley)

    What about your sorting code. Can you paste that too.

    Thread Starter davidrevo

    (@davidrevo)

    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');
        }
    }
    Plugin Author Mike Jolley

    (@mikejolley)

    You should be comparing the key with == $orderby, not the label. e.g. job_deadline

    Thread Starter davidrevo

    (@davidrevo)

    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

    Plugin Author Mike Jolley

    (@mikejolley)

    Not sure why, but if ( ‘ClosingDate’ == $orderby ) { worked for me.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Column Sorting on Admin Page’ is closed to new replies.