Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Federico Citi

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!

3 weeks, 2 days ago
Federico Citi

Sorry, in lines 3-5, I forgot to remove a function that I used during development… please disregard it.

3 weeks, 2 days ago
Stefan van den Dungen Gronovius
Developer

Thanks for your message.
First of all, if the stored value is an integer that refers to a post ID, then the custom field column should work with the Field Type set to ‘Post’. When you choose that field type, there will be a setting that allows you to set what field you would to display, and it is set by default to the post title.

And based on your custom code so far, I don’t see why that option should not work.

While the hook to alter the value does work, it will be far more advanced to show a list of titles for the Filter and editing drop-downs as well. And again, all the logic int he Post field type should do that already for you.

Can you try to use the Post Field Type again and see if it works. You’ll need to deactivate your custom hook to test it. If it does not work, we could try to figure out why it does not work. Feel free to contact me by email in that case at support@admincolumns.com

3 weeks, 1 day ago

You must be logged in to reply to this topic.