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;
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.