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

  $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();    
    $sub_field_3 = get_sub_field('sub_field_3');
    //do something with the saved variable


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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

async function in JavaScriptJavaScriptModules and its Core features in JavaScriptJavaScriptHow to get Current Year, Month and Date in JavaScript?JavaScriptWhat are CSS Specificity Rules and how does browser apply them?CSSThe Drupal flowDrupalQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQLThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresHow to Recognize an Array in JavaScript?JavaScriptLearn to create profile card using HTML and CSSCSSHow to get ACF values from custom post type?WordPressHow to remove a specific item from an array in JavaScript?JavaScriptUNION and UNION ALL in PostgresPostgres