NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Oct 09 2022
Estimated reading time : 6 minutes, 9 seconds - 560 words
Share the post "Dynamically Completing Selection Fields"
With this tutorial, we will show you how to complete fields dynamically for custom post types.
For this we’ll need to use the ACF: Advanced Custom Fields plugin.
This popular extension allows users to create custom fields for posts, pages and much more.
Create group fields with ACF
We can create group fields and add the desired fields within these groups. Certain fields are available in the free version, and the Pro version .
For example, the Repeater field is available in the Pro version.
List of fields avalable in ACF
Here’s the list of custom fields available in the free and Pro versions.
Basic
- Text
- Text fields
- Numbers
- Set
- URL
- Password
Content
- Image
- Files
- Wysiwyg editor
- oEmbed content
- Gallery
Choices
- Drop down list
- Checkboxes
- Radio button
- Button group
- True/False
Relational
- Link
- Objet de la publication
- Hyperlink
- Relation
- Taxonomy
- Account
JQuery
- Google Maps
- Date selector
- Date & time selector
- Time selector
- Color selector
Layout
- Message
- Accordion
- Tab
- Group
- Repeater
- Flexible content
- Clone
For our tutorial, we will create a new group and add a custom selectable field.
This group of fields can be displayed wherever you need it.
Furthermore, within this field, you can add custom values or display post results found within your dynamic selection field.
Display a selection of posts with ACF
First we need to use a WP Query displayed with a acf/load_field/filter.
We will also use an argument called name which will serve as the name of the ACF custom dynamic field that you have created: acf/load_field/name=test.
Replace it with the name of your custom ACF field.
add_filter('acf/load_field/name=test', 'ThemespresspopulateActivites'); function ThemespresspopulateActivites($field) { $field['choices'] = array(); wp_reset_query(); $query = new WP_Query(array( 'post_type' => 'activites', 'order' => 'ASC', 'orderby' => 'title', 'posts_per_page' => -1, )); $field['choices'] = array(); foreach ($query->posts as $room_id => $matched_room) { $choices[$matched_room->ID] = $matched_room->post_title; } if (is_array($choices)) { foreach ($choices as $key => $choice) { $field['choices'][$key] = $choice; } } wp_reset_query(); return $field; }
Display a selection of result within an ACF selection field for Multisite
Let’s take a WP Query from a multisite website, we will use the switch_to_blog function.
In our query, we will fetch the website’s ID found within the multisite. (switch_to_blog(2))
It will then display inside our custom field all posts with an ID 2 and sorted by ascending title.
Finally replace the name of the field with yours: (acf/load_field/name=test)
add_filter('acf/load_field/name=test', 'ThemespresspopulateActivitesMultisite'); function ThemespresspopulateActivitesMultisite($field) { $field['choices'] = array(); wp_reset_query(); switch_to_blog(2); $querytwo = new WP_Query(array( 'post_type' => 'post', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'title', )); $field['choices'] = array(); foreach ($querytwo->posts as $room_id => $matched_room) { $choices[$matched_room->ID] = $matched_room->post_title; } if (is_array($choices)) { foreach ($choices as $key => $choice) { $field['choices'][$key] = $choice; } } restore_current_blog(); return $field; }
ACF – Advanced Custom Fields is an efficient plugin that will diplays your data related to posts, pages, post types, etc…
If we don’t want to add a user field from ACF, we can create a selection field and create a query to display users with a specfic role.
You can change the role in the example and why not add additional roles in the author, subscriber, editor, contributor array.
add_filter('acf/load_field/name=partenaire', 'themepresspopulateUserGroups'); function themepresspopulateUserGroups( $field ){ // reset choices $field['choices'] = array(); $users = get_users(array( 'role__in' => array( 'partenaire' ) ) ); foreach ($users as $user) { $field['choices'][ $user->ID ] = $user->display_name; } return $field; }
Finally, we need to change the name of the field: acf/load_field/name=partenaire
In conclusion, ACF – Advanced Custom Fields is a very useful plugin that will let you display data from posts, post types, pages, etc…