Carbon-fields: How to use title hook to edit association item titles

Created on 13 Dec 2017  路  2Comments  路  Source: htmlburger/carbon-fields

Hi there,

I've been browsing the hooks page, and I'd like to edit the titles of my association field items on the dashboard.

https://carbonfields.net/docs/advanced-topics-hooks/?crb_version=2-1-0

I tried using the carbon_fields_association_field_title hook but it didn't seem to work quite like the only other hook I've tried for query options:

add_filter( 'carbon_fields_association_field_options_[field_name]_[post_type]_[cpt_name/sub_type]', function( $query_arguments ) {
                $query_arguments['meta_query'] = [
                    'future_movies' => [
                        'key'     => '_movie_start_date',
                        'value'   => date( 'Y-m-d H:i:s' ),
                        'type'    => 'date',
                        'compare' => '>=',
                    ],
                ];

                return $query_arguments;
            } );

Since the above hook accepts an array of options, I can return the array $query_arguments. But for the title hook, if I just want to add say, the first category name to all association titles, I just want to return the $title once I'm done without having to specify an $id. Is that possible?

enhancement [type] question

Most helpful comment

@atanas-angelov-dev That works perfectly. Just what I needed. Thank you. It'd be great to see some of these examples on the docs. Perhaps I'll fork the docs and make a contribution. 馃槃

All 2 comments

Hi @RachelRVasquez ,

The hook only expects you to return a title - the other arguments are there to help you out with extra information should you need it.

Here's an example:

add_filter( 'carbon_fields_association_field_title', function( $title, $field_name, $id, $type, $subtype ) {
    $field = 'related_posts'; // the field you wish to filter
    if ( $field_name !== $field ) {
        return $title; // not the field we want to filter - return the title unmodified
    }
    return 'Custom Prefix: ' . $title; // add a custom prefix to all options
}, 10, 5 );

This filter will cause all of your options to have a "Custom Prefix: " prefix.

@atanas-angelov-dev That works perfectly. Just what I needed. Thank you. It'd be great to see some of these examples on the docs. Perhaps I'll fork the docs and make a contribution. 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olegburakov picture olegburakov  路  3Comments

AlexBa picture AlexBa  路  3Comments

proweb picture proweb  路  3Comments

jonwaldstein picture jonwaldstein  路  3Comments

Mick00 picture Mick00  路  3Comments