Support

Account

Home Forums General Issues Difference between prepare_field and load_field

Unread

Difference between prepare_field and load_field

  • I have a staff directory on another site in my multisite. I’d like to return Staff members within my select field (as I know I can’t do this with post-object/relationship fields). But I cannot get it to work, though when I print out the query, it’s returning what I need it to.

    function MemberProfilesQuery( $field ) {
        
        switch_to_blog( 8 );
    
        // Declare empty array
        $field['choices'] = array();
        
        $args = array(
            'post_type'         => 'staff',
            'posts_per_page'    => -1,
            'orderby'           => 'meta_value',
            'order'             => 'ASC',
            'meta_key'          => 'last_name'
        );
        
        $staffMembers = new WP_Query( $args );
        
        if( $staffMembers->have_posts() ):
        
        	while( $staffMembers->have_posts() ) : $staffMembers->the_post();
                
                $id = get_the_ID();
                $preferredName = get_field( 'preferred_name', $id );  
                $lastName = get_field( 'last_name', $id );
                $name = $lastName . ', ' . $preferredName;
    
                $field['choices'][$id] = $name;
        
        	endwhile;
    
        endif;
        
        return $field;
    
        wp_reset_query();
        
        restore_current_blog();
        
    }
    
    add_filter('acf/load_field/key=field_5ed9cff65f654', 'staffMemberProfilesQuery');

    Any ideas as to why this wouldn’t work?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.