Support

Account

Home Forums General Issues Difference between prepare_field and load_field

Solving

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?

  • I just stumbled upon this 2 years old question searching for something else but for whom it may concern there are couple of things that can be immediately identified as weird:

    1. These 2 lines will never get executed because they are after the return call:

    
    
        wp_reset_query();
        restore_current_blog();
    

    2. The name of the function in the add_filter call is staffMemberProfilesQuery while the name of the function defined at the beginning of the code block is MemberProfilesQuery

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Difference between prepare_field and load_field’ is closed to new replies.