Support

Account

Home Forums ACF PRO Query posts to create array with field values

Helping

Query posts to create array with field values

  • I’ve been noodling this for a while and haven’t found an elegant solution.

    I have a custom post type (Committee) that has two ACF fields: committee_name and default_location.

    I’d like to query all the Committee pages and end up with an array using the values of those two fields: committee_name => default_location.

    Ex:
    Rules => 300 W. 95th St.
    Executive => The Baxter Building
    etc.

    I’m certain there’s an easy way to do this. I have no problems creating the query, but I’m self-taught and creating the array escapes me. Thanks for any help you may be able to provide.

  • I should have added my current, non-working code. Please be gentle.

    $committees = array( 
        'numberposts' => -1,
        'post_type' => 'committee'
    ); 
    
    $query = new WP_Query($committees);
    $location_array = array();
    
    if( $query->have_posts() ): 
            while( $query->have_posts() ): $query->the_post();
    $location_array[get_field('committee_name')] = get_field('committee_location'); 
        endwhile;
        wp_reset_query();
    endif; 
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query posts to create array with field values’ is closed to new replies.