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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to use @supports rule in CSS?CSSHow to add Go Back button in Swift 5?SwiftHow to detect if browser supports WebP format on server side PHP script?PHPEffects in ReactReactComponents and Props in ReactReactHow to switch dark and light themes using pure CSS?CSSColleague UI Basics: The Context AreaColleagueLEFT JOIN in PostgresPostgresHow to create a Progress Bar using JavaScript?JavaScriptWordPress: How to display fields from ACF Flexible Contents?WordPressJavaScript Number MethodsJavaScriptHow to create a for loop in SCSS (Sass)?SCSS