Displaying Post Titles Instead of IDs in a Custom Field Column and Filter
Hi,
I’m seeking some help with displaying a custom field in a column.
Context:
I have a custom field called “eitm_consortium”. This custom field was not created with ACF. It stores an integer value, which represents the ID of a CPT called “consortium”.
Using Admin Columns Pro, I’ve added a column with the following configuration:
Type: Custom Field
Field: “eitm_consortium”
Field Type: Default (I’ve tried other options as well)
With this setup, the column displays the IDs of the CPT “consortium”, and the filter dropdown also lists these IDs.
My Goal:
I would like to replace the IDs with the post titles.
What I’ve Managed So Far:
For the column, I was able to achieve this with the following code:
add_filter( 'ac/column/value', function ( $value, $id, AC\Column $column ) {
if ( $column instanceof ACP\Column\CustomField && 'eitm_consortium' === $column->get_meta_key() ) {
if( 2 == get_current_user_id() ) {
ray(get_class($column))->label('Column class');
}
if ( $value ) {
$consortium_post = get_post( $value );
if ( $consortium_post ) {
return '<a href="' . $consortium_post->permalink . '" target="_blank">' . $consortium_post->post_title . '</a>';
} else {
return __('No Consortium');
}
} else {
return __('No Consortium');
}
}
return $value;
}, 10, 3 );
This successfully displays the post titles in the column instead of the IDs.
The Problem:
I am having trouble figuring out how to do the same for the filter dropdown. Currently, it lists the IDs of the “consortium” posts, but I need it to list the post titles instead.
Could you please assist me with this?
Thank you very much for your help!
You must be logged in to reply to this topic.