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 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

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
How to enable GD library support for PHP on windows server?PHPHow to create a cross-browser smooth scrolling with jQuery?jQueryHow to overwrite file contents with new content in PHP?PHPGap in Flex?CSSuser_can vs current_user_can in WordPressWordPressHow to create a Recent Posts function in WordPress?WordPressIf statement shorthand examples in JavaScriptJavaScriptKanban vs Scrum: The two frameworks of agile principlesMiscellaneousFULL OUTER JOIN in PostgresPostgresOrder By and Group By in PostgresPostgresHow get_template_part helps write reusable code in WordPress?WordPressArguments in @mixin rules in SCSS (Sass)SCSS