Support

Account

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

Solving

Get ALL fields for ALL posts in one array

  • Hi,

    New to ACF, intermediate PhP/Mysql skills

    I’m trying to get all field data for all posts into one array that I can then parse and retrieve all data connected to each post.
    Something like:

    Post 1: ACF field #1, ACF field #2, ACF field #3
    Post 2: ACF field #1, ACF field #2, ACF field #3
    Post 3: ACF field #1, ACF field #2, ACF field #3
    Post 3: ACF field #1, ACF field #2, ACF field #3
    etc…

    I’ve tried

    get_fields() and get_field_objects

    But I only get:
    Post 1: ACF field #1, ACF field #2, ACF field #3

    I’ve tried the following query, but it spits out a row for each ACF field, so I end up with 16 rows for each post, as opposed to one row for each post with post, post_meta, and ACF info:

    $sql = $wpdb->get_results('SELECT post_title, ID, post_id, post_status, guid, post_id, meta_key, meta_value FROM ' . $wpdb->posts . ' JOIN ' . $wpdb->postmeta . ' ON _XXX_posts.ID = _XXX_postmeta.post_id WHERE _XXX_posts.post_status = "publish" AND _XXX_posts.post_type="post"');

    So, I get this:
    Post 1: ACF field #1
    Post 1: ACF field #2
    Post 1: ACF field #3
    Post 2: ACF field #1
    Post 2: ACF field #2
    Post 2: ACF field #3
    Post 3: ACF field #1
    Post 3: ACF field #2
    Post 3: ACF field #3

    Hope this makes sense.
    Thanx for taking a look!!

  • 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;
    
  • So, you’re officially going above and beyond here. This is super helpful for a Joomla guy who knows next to nothing about the WP API.

    This works for all ACF field data, but I’m needing all ACF data, AND all the WP post data.

    Can you help me get that data into the array as well?

    Thanx so much for helping me with this.

    Mark

  • In that case, you’ll want to use the WP Post Object. You can then add the ACF fields array to each post. I’ve tried to make the comments as verbose as possible. I hope this helps!

    
    <?php 
    
    // Get arguments for all posts
    $args = array( 
    	'posts_per_page' => -1,
    );
    
    // Create the array for all posts AND all ACF fields
    $all_posts = array();
    $all_acf_fields = 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 WP Post Object
    		$fields = get_post();
    
    		// Get ACF fields
    		$acf_fields = get_fields();
    
    		// Push each $fields object into the $all_posts array
    		array_push($all_posts, $fields);
    
    		// Push each ACF array into the $all_acf_fields array
    		array_push($all_acf_fields, $acf_fields);
    
    	endwhile;
    
    	// Restore original Post Data
    	wp_reset_postdata();
    
    	// Arbitrary array counter
    	$i = 0;
    
    	/* 
    	 * The magic
    	 * For every post in the array of $all_posts,
    	 * add a new object key of "acf_fields" to the post object.
    	 * 
    	 * This contains the array of ACF fields for that post.
    	 *
    	 * Lastly, increment the array for each post.
    	 */
    				
    	foreach ($all_posts as $p):
    		$p->acf_fields = $all_acf_fields[$i];
    		array_push($all_posts, $p);
    		$i++;
    	endforeach;
    
    	// Print the result here and do what you choose
    	echo '<pre>';
    	print_r($all_posts);
    	echo '</pre>';
    
    endif;
    
    ?>
    
  • Again, thanx so much for the help here.

    This almost does it – but for some reason, the code is generating four main arrays, but I only have two posts in my database. There should only be 2 WP_Post Object arrays, right? Here’s the output I got:

    Array
    (
        [0] => WP_Post Object
            (
                [ID] => 194
                [post_author] => 2
                [post_date] => 2019-03-01 16:29:16
                [post_date_gmt] => 2019-03-01 16:29:16
                [post_content] => My Neighbor's house - sweet yard, Victorian feng shui
                [post_title] => 2150 Lafayette Street Denver, 80205
                [post_excerpt] => 
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] => 
                [post_name] => 2150-lafayette-street-denver-80205
                [to_ping] => 
                [pinged] => 
                [post_modified] => 2019-03-01 16:29:16
                [post_modified_gmt] => 2019-03-01 16:29:16
                [post_content_filtered] => 
                [post_parent] => 0
                [guid] => https://locatedenver.com/?p=194
                [menu_order] => 0
                [post_type] => post
                [post_mime_type] => 
                [comment_count] => 0
                [filter] => raw
                [acf_fields] => Array
                    (
                        [call] => No
                        [hot] => Yes
                    )
    
            )
    
        [1] => WP_Post Object
            (
                [ID] => 188
                [post_author] => 2
                [post_date] => 2019-03-01 16:21:50
                [post_date_gmt] => 2019-03-01 16:21:50
                [post_content] => My house, tiny back yard, super loud, bonsai trees, shitty grass
                [post_title] => 2144 Lafayette Street Denver, 80205
                [post_excerpt] => 
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] => 
                [post_name] => 2144-lafayette-street-denver-80205
                [to_ping] => 
                [pinged] => 
                [post_modified] => 2019-03-01 16:26:01
                [post_modified_gmt] => 2019-03-01 16:26:01
                [post_content_filtered] => 
                [post_parent] => 0
                [guid] => https://locatedenver.com/?p=188
                [menu_order] => 0
                [post_type] => post
                [post_mime_type] => 
                [comment_count] => 0
                [filter] => raw
                [acf_fields] => Array
                    (
                        [call] => Yes
                        [hot] => Yes
                    )
    
            )
    
        [2] => WP_Post Object
            (
                [ID] => 194
                [post_author] => 2
                [post_date] => 2019-03-01 16:29:16
                [post_date_gmt] => 2019-03-01 16:29:16
                [post_content] => My Neighbor's house - sweet yard, Victorian feng shui
                [post_title] => 2150 Lafayette Street Denver, 80205
                [post_excerpt] => 
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] => 
                [post_name] => 2150-lafayette-street-denver-80205
                [to_ping] => 
                [pinged] => 
                [post_modified] => 2019-03-01 16:29:16
                [post_modified_gmt] => 2019-03-01 16:29:16
                [post_content_filtered] => 
                [post_parent] => 0
                [guid] => https://locatedenver.com/?p=194
                [menu_order] => 0
                [post_type] => post
                [post_mime_type] => 
                [comment_count] => 0
                [filter] => raw
                [acf_fields] => Array
                    (
                        [call] => No
                        [hot] => Yes
                    )
    
            )
    
        [3] => WP_Post Object
            (
                [ID] => 188
                [post_author] => 2
                [post_date] => 2019-03-01 16:21:50
                [post_date_gmt] => 2019-03-01 16:21:50
                [post_content] => My house, tiny back yard, super loud, bonsai trees, shitty grass
                [post_title] => 2144 Lafayette Street Denver, 80205
                [post_excerpt] => 
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] => 
                [post_name] => 2144-lafayette-street-denver-80205
                [to_ping] => 
                [pinged] => 
                [post_modified] => 2019-03-01 16:26:01
                [post_modified_gmt] => 2019-03-01 16:26:01
                [post_content_filtered] => 
                [post_parent] => 0
                [guid] => https://locatedenver.com/?p=188
                [menu_order] => 0
                [post_type] => post
                [post_mime_type] => 
                [comment_count] => 0
                [filter] => raw
                [acf_fields] => Array
                    (
                        [call] => Yes
                        [hot] => Yes
                    )
    
            )
    
    )
  • It’s weird, even if I delete all ACF fields from WP Admin area, and clean all ACF data from post_meta table in the DB, and all post data from wp_posts, except for the two published posts, I still get 4 arrays…

    If I add another post, I get 6 arrays, etc.

    Not sure where the code you’ve provided loops twice through each post.

    Thanx again for your help!!!

    Mark

  • I know that, at the end of your code, in this loop, is where things seem to get doubled up:

    	foreach ($all_posts as $p):
    		$p->acf_fields = $all_acf_fields[$x];
    		array_push($all_posts, $p);
    		    $x++;
    	endforeach;

    If I comment that out, and print $all_posts or $all_acf_fields, it shows the right number of arrays, ie, one array for each post

  • This fixed it:

    foreach ($all_posts as $p):
    		$p->acf_fields = $all_acf_fields[$x];
    		//array_push($all_posts, $p);
    		$x++;
    	endforeach;

    Thanx again for your help!!!

  • Hey there,

    Sorry for the late reply, been swamped the past few days. Glad you figured it out!

    Best,
    Josh

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

The topic ‘Get ALL fields for ALL posts in one array’ is closed to new replies.