WordPress教程:如何将ACF自定义文章类型关联到WordPress账户
Got it, let's solve this together. You’ve already set up your members custom post type, and now you want to restrict an ACF user field to only show users with the subscriber role. Here’s exactly how to do it:
1. Filter ACF User Field to Only Display Subscribers
First, if you haven’t already, create an ACF User field (let’s say you name it linked_subscriber for this example). To limit this field to just subscribers, we’ll use an ACF filter to tweak the user query that populates the field options.
Add this code to your theme’s functions.php file or a custom plugin:
// Restrict ACF User field to only show Subscriber role users function acf_limit_user_field_to_subscribers( $args, $field, $post_id ) { // Target only your specific ACF user field (optional but recommended) if ( $field['key'] === 'field_your_field_key' ) { // Set the user role to subscriber $args['role'] = 'subscriber'; // Optional: Sort users by display name (ascending order) $args['orderby'] = 'display_name'; $args['order'] = 'ASC'; } return $args; } add_filter( 'acf/fields/user/query', 'acf_limit_user_field_to_subscribers', 10, 3 );
- Replace
field_your_field_keywith the actual key of your ACF user field (you can find this in the ACF field editor under "Field Key"). - This filter modifies the query that pulls users for your field, so only subscribers will appear in the dropdown.
2. Attach the ACF Field to Your Members Post Type
Make sure your ACF field group is set to display only on the members post type:
- Go to your ACF Field Group settings.
- Under Location, add a rule:
Post Type is equal to Members. - Save the field group—now it’ll only show up when you edit a
memberpost.
3. (Optional) Show Linked Subscriber Info in Your Custom Columns
You already have custom columns set up for your members list. If you want to pull info directly from the linked subscriber (instead of using post meta), update your fill_member_columns function like this:
function fill_member_columns( $column, $post_id ) { switch ( $column ) { case 'contactperson': // Get the linked subscriber ID from ACF $subscriber_id = get_field( 'linked_subscriber', $post_id ); if ( $subscriber_id ) { $subscriber = get_user_by( 'ID', $subscriber_id ); echo esc_html( $subscriber->display_name ); } break; case 'phone_number': // If you store phone in user meta, pull it here instead $subscriber_id = get_field( 'linked_subscriber', $post_id ); if ( $subscriber_id ) { echo esc_html( get_user_meta( $subscriber_id, 'phone_number', true ) ); } else { echo esc_html( get_post_meta( $post_id, 'phone_number', true ) ); } break; case 'email': $subscriber_id = get_field( 'linked_subscriber', $post_id ); if ( $subscriber_id ) { $subscriber = get_user_by( 'ID', $subscriber_id ); echo esc_html( $subscriber->user_email ); } else { echo esc_html( get_post_meta( $post_id, 'email', true ) ); } break; } } add_action( 'manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );
- Replace
linked_subscriberwith your actual ACF field name. - Using
esc_html()ensures your output is safe from XSS vulnerabilities—always a good practice!
That’s it! Now when you edit a member post, your ACF user field will only list subscribers, and you can display their details directly in the admin list view.
内容的提问来源于stack exchange,提问作者Asta

