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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Composing and Extracting Components in ReactReactArray.from() Method in JavaScriptJavaScriptContent types in DrupalDrupalIs there a difference between SCSS and Sass?SCSSSolution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’SwiftMIN, MAX, COUNT, AVG and SUM in SQLSQL/MySQLHow to add Lifecycle Methods to a Class in React?ReactHow can one check to see if a remote file exists using PHP?PHPHow to convert datetime to date format in JavaScript?JavaScriptHow to scroll contents of HTML body horizontally and vertically using JavaScript?JavaScriptWhat does Containment mean in React?ReactHow to uninstall Cocoapods from the Mac OS?Miscellaneous