Add a sortable WordPress post list column for the ACF True/False field

If you’ve ever had to use Advanced Custom Fields (ACF) in WordPress then you’d be aware that when you add a new ACF field to a post or custom post type and set a default value then that value isn’t actually saved until a post is published or updated. This can be an issue if you’ve got a site with thousands of posts and you want to have a sortable column in the WordPress administration area. I just had to work around this issue and it took me about 45 minutes to write a solution that worked so I figured it was worthy of a blog post as I’m sure future Bronson will probably need to do this again in the future.

Firstly, let’s start with the easy parts. We’ll add the heading for the Column in the WordPress admin area. I’m adding this to the Posts list table so here’s my code for that:

/**
* Add the Auto Publish Updates setting to the Post list in WordPress.
* @param $columns
*
* @return array
*/
function add_acf_columns( $columns ) {
	$columns = array_merge(
		$columns,
		array(
			'auto_publish_updates' => __( 'Auto Publish', 'apl-data-mapping' )
		),
	);
	return $columns;
}
add_filter( 'manage_post_posts_columns', 'add_acf_columns' );

Next up we want to show the values of Yes and No in the columns:

/**
* Add Auto Publish value to Post list.
*
* @param $column
* @param $post_id
*
* @return void
*/
function post_custom_column( $column, $post_id ) {
	switch ( $column ) {
	case 'auto_publish_updates':
		$auto_update = get_field( 'auto_publish_updates', $post_id );
		if ( true === $auto_update or null === $auto_update ) {
		echo esc_html__( 'Yes', 'apl-data-mapping' );
		} else {
		echo esc_html__( 'No', 'apl-data-mapping' );
		}
		break;
	}
}
add_action( 'manage_post_posts_custom_column', 'post_custom_column', 10, 2 );

Next up we need to allow the ability to make that column sortable:

/**
* A function to make the Auto Updates column sortable.
* @param $columns
*
* @return mixed
*/
function sortable_auto_publish_updates_column( $columns ) {
	$columns['auto_publish_updates'] = 'auto_publish_updates';
	return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'sortable_auto_publish_updates_column' );

Now to the tricky part which is to intercept the `orderby` query and wrangle it to our needs:

/**
* Add a function to sort the Auto Update column.
*
* @param $query
*
* @return void
*/
function sort_auto_publish_updates( $query ) {
	if ( ! is_admin() ) {
		return;
	}
	$orderby = $query->get( 'orderby' );
	if ( 'auto_publish_updates' === $orderby ) {
		$meta_query = array(
			'relation' => 'OR',
			array(
					'key' => 'auto_publish_updates',
				'value' => 0,
				'type' => '=',
			),
			array(
				'key' => 'auto_publish_updates',
				// Posts that haven't been saved won't have ACF data in this field.
				'compare' => 'NOT EXISTS',
			),
		);
	$query->set( 'meta_query', $meta_query );
	$query->set( 'orderby', 'meta_value_num' );
	}
}
add_action( 'pre_get_posts', 'sort_auto_publish_updates' );

And now you have a sortable True/False ACF field in your WordPress admin area:

Bronson Quick
0

Leave a Reply

%d bloggers like this: