Support

Account

Home Forums General Issues Get ALL fields for ALL posts in one array Reply To: Get ALL fields for ALL posts in one array

  • The following code will get all posts, get an array of all the fields for the current post in the loop, and push those arrays into a new array called $all_posts. Hope this helps!

    
    // Get arguments for all posts
    $args = array( 
    	'posts_per_page' => -1,
    );
    
    // Create the array for all posts
    $all_posts = array();
    
    // Set up the query
    $the_query = new WP_Query( $args );
    
    // Setup the loop
    if ( $the_query->have_posts() ):
    
    	while ( $the_query->have_posts() ): $the_query->the_post();
    
    		// Get all fields
    		$fields = get_fields();
    
    		// Push each $fields array into the $all_posts array
    		array_push($all_posts, $fields);
    
    	endwhile;
    
    	// Restore original Post Data
    	wp_reset_postdata();
    
    	// Print the result here and do what you choose
    	print_r($all_posts);
    
    endif;