Support

Search results for ""

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

Custom orderby for Sort, Filter of ACF post_object field

Hi, I have a CPT “Artworks” which is related to a CPT “Artist” using an ACF post object field. Right now the Artist column returns the title (which is first_name last_name) and therefore alphabetizes by the first names. Is there a way to use the ac/column/value filter so that I can replace the post object field value for Sorting and Filtering so that it alphabetizes by the artist’s custom fields last_name and first_name? I’m using this code but it doesn’t work (and I think that it would actually change how it’s displayed in the list, not the Sort/Filter).

function ac_my_acf_column_value_example( $value, $id, $column ) {
	if ( $column instanceof ACF ) {
		$meta_key = $column->get_meta_key(); // This gets the ACF field key
		$meta_value = $column->get_meta_value(); // This gets the ACF field key
		$acf_field = $column->get_acf_field(); // Gets an ACF object

		if( 'artists_name' == $acf_field  ){
			// Alter the display $value
			$value = get_field('artist_last_name', $meta_value);
		}
	}
	return $value;
}
add_filter( 'ac/column/value', 'ac_my_acf_column_value_example', 10, 3 );
1 year ago
Stefan
Developer

Hello

Using the filter indeed should change the value for display but not for sorting and filtering.
You could change the items in the filter drop down by repopulating the options by using this filter:

acp/filtering/dropdown_args

Sorting will be more difficult since you want to sort on related data. If you write your own column, you could write your own sorting model to do it. Unfortunately, we don’t have any filter to do that. We do have a sorting model that does sort on the value out of the box, but this can be heavy on performance because it first try to get the value for each post from the database and try to sort on it next.

If you’re interested in writing your own column, extending our meta column already does quite a lot. This is an example snippet to get you started.

/**
 * @param \AC\ListScreen $list_screen
 */
function ac_register_column_META_EXAMPLE( AC\ListScreen $list_screen ) {

	class AC_Column_Meta_Example extends ACP\Column\Meta
		implements ACP\Sorting\Sortable {

		public function __construct() {
			$this->set_type( 'column-custom_meta_example' );
			$this->set_label( 'AC Meta Example' );
		}

		public function get_meta_key() {
			return 'wpcf-types_date';
		}

		public function get_value( $post_id ) {
			$raw_value = $this->get_raw_value( $post_id );

			// Custom Display logic

			return $raw_value;
		}

		public function sorting() {
			return new ACP\Sorting\Model\Value( $this ); //This one sorts on the value returned by get_value -> Heavy on performance
			return parent::sorting(); // This one sorts on the raw_value or the meta value in the database
		}

	}

	$list_screen->register_column_type( new AC_Column_Meta_Example );
}
add_action( 'ac/column_types', 'ac_register_column_META_EXAMPLE' );
1 year ago
dolly

Thanks, will look into that. Since there are less than 100 CPT to sort it shouldn’t be too taxing on performance.

1 year ago

You must be logged in to reply to this topic.