Yogesh Chauhan's Blog

WordPress: How to get ACF field values from another post?

in WordPress on February 22, 2021

Printing array from the current post and page is pretty straight forward. Similarly, printing repeater in the same post or page is relatively simple.

But when it comes to getting ACF fields values from another post, it becomes a bit tricky. Let’s go through some examples.

Printing a field from another post



<?php $other_page = 12; ?>
<p><?php the_field('field_name', $other_page); ?></p>


Saving a field from another post to a variable



<?php 
  $other_page = 12;
  $variable = get_field('field_name', $other_page);
  //do something with the saved variable
?>


Getting repeater values from another post

For repeater values, the_sub_field and get_sub_field don’t need a post id parameter like the_field and get_field.



if( have_rows('repeater_field_name', $other_page) ):
  while( have_rows('repeater_field_name', $other_page) ): the_row();    
    the_sub_field('sub_field_1');
    the_sub_field('sub_field_2');
    $sub_field_3 = get_sub_field('sub_field_3');
    //do something with the saved variable
  endwhile;
endif;



Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Is there a way to do a FULL OUTER JOIN in MySQL?SQL/MySQLHow to convert an object from API to JSON array in Angular 9?AngularWordPress: How to display fields from ACF Flexible Contents?WordPressAngular: Templates, directives, data binding, Services and dependency injectionAngularArray destructuring and Object destructuring in JavaScriptJavaScriptWordPress: How to display slider repeater fields in ACF?WordPressHow to select multiple values in React dropdown?ReactJavaScript Number MethodsJavaScriptSQL Left JoinSQL/MySQLHow to insert Bootstrap 4 in Angular 9 app?AngularDROP DATABASE (remove a database) from PostgresPostgresFilling a button background from left to right using CSSCSS