Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use title hook to edit association item titles #442

Closed
RachelRVasquez opened this issue Dec 13, 2017 · 2 comments
Closed

How to use title hook to edit association item titles #442

RachelRVasquez opened this issue Dec 13, 2017 · 2 comments

Comments

@RachelRVasquez
Copy link

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?

@atanas-dev atanas-dev added the good first issue Feel free to contribute a PR to resolve issues like this one. label Dec 14, 2017
@atanas-dev
Copy link
Contributor

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.

@RachelRVasquez
Copy link
Author

@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. 😄

@atanas-dev atanas-dev added [type] question and removed good first issue Feel free to contribute a PR to resolve issues like this one. labels Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants