YogeshChauhan . com

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;


amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

What Is a Graph Database?MiscHow to set default timezone using PHP?PHPClearwater Seafoods – B2C in ChinaMiscHow to show and hide an element on click in React?ReactQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQLHow to check if radio button is checked or not using JavaScript?JavaScriptLearn how to add Scroll Indicator using CSS and JavaScript?CSSHow to swap images on hover using CSS?CSSA list of wp-cli commands to use via SSHWordPressWhat are Web services?MiscSimple Page Hit Counter in PHPPHPHow to create a link tag button using CSS?CSS