Association Field: Limit choices by date or post status, and set an order
Hi,
I just wanted to know if there are any plans to implement ordering in the association field? Right now, if I set the association field to pull a custom post type for example, while I can set a limit for the user for how many they can choose, I can't set a limit for the choices they have. It'd be cool to limit these by dates or post status.
The choices also appear from the most recent to oldest as far as published date I assume? It'd be awesome to be able to order these ascending/descending by date or alphabetically to make it easier for the user to find what they need without having to necessarily use the search field.
Just dropping in ideas. Love carbon fields and thanks for all your hard work thus far. 👍
Hi @RachelRVasquez ,
The association field provides a filter for its query arguments so that you can change them as you need:
'carbon_fields_association_field_options_{FIELD_NAME}_{TYPE}_{SUBTYPE}'
If we have a field with a name of "related_posts" which loads posts of type "post", then our filter would be:
add_filter( 'carbon_fields_association_field_options_related_posts_post_post', function( $query_arguments ) {
// Order posts by title alphabetically, for example:
$query_arguments['orderby'] = 'post_title';
$query_arguments['order'] = 'asc';
return $query_arguments;
} );
There are also a few other association filters that you may find useful listed here: https://carbonfields.net/docs/advanced-topics-hooks/?crb_version=2-1-0
Just dropping in ideas. Love carbon fields and thanks for all your hard work thus far. 👍
Thank you for the support!
😮 I had no idea! I'm going to try this! Thanks so much. 👍 😄
@atanas-angelov-dev I got it working! I realized that since I was using a custom post type, I needed to use carbon_fields_association_field_options_[field_name]_[post_type]_[cpt_name] instead. Thanks!
Can I please mention that it isn't immediately obvious in the documentation that the word "options" here refers to the arguments of the WP_Query constructor? Please make that clear by adding to the paragraph that reads:
Allows you to modify the available options of the association field with name
{name}from the{type}type and of the{subtype}subtype where applicable. .
Thank you!
Hi @ssimo3lsuhsc,
Thank you for the feedback.
I've added information about the $options parameter to the documentation, so it will be available in the next release.