0
votes

I'm trying to retrieve the value of the fields I created via Advanced Custom Fields (ACF).

At the moment, I have 3 field groups. However, when I try to retrieve the values of the fields, I'm only able to retrieve the value of one of the field group. I can't retrieve the value of the other 2 field groups.

I've tried the_field(), get_post_meta(), get_field(), get_field($post->ID),get_field('field_name', post_id) but all returns either NULL or False.

I've also tried get_fields() and this one returns the array of one of the field group only.

When I try to var_dump the variable to verify if the values were retrieved, this is what I get instead. error screenshot

https://prnt.sc/ptvjsa (link to the screenshot)

<?php
/*
    Template Name: Home Page
*/ 

// Advanced Custom Fields (Product Categories)
$category_section_desc = get_field('our_product_description'); // 1 of the 3 field groups I created

$category_title = get_field('category_title'); // 2nd of the 3 field groups I created

$product_name = get_field('product_name'); // 3rd of the 3 field groups I created


get_header();

var_dump($category_title);
var_dump($product_name);
?>

My goal is to retrieve the values of the fields from the other field groups and display them all together in 1 page - the home page.

1
Do you have your home page set in Settings > Reading? Do you actually have data in those fields on whatever page is set as the home page? - disinfor
can you specify how you declared your fields? so we know if they were declared properly and if some of them are sub fields.. - denisey
@disinfor, Yes to both. - Franz
@denisey, inside ACF menu, I added 3 field groups and then connected each field group to 3 custom post types as well. I then added data to them by clicking the Add new buttons. I can see the data listed but when I try to retrieve them, I'm getting a NULL or FALSE return. - Franz
Post screen shots of your field group (in ACF) and the fields on the particular page - the home page. - disinfor

1 Answers

0
votes

I finally figured it out! Thanks for the help though guys!

What I did is that I enclosed it with the WP_Query loop.

<?php 
  $category = "Category Name";
  $loop = new WP_Query( array('post_type' => 'products', 'orderby' => 'post_id', 'order' => 'ASC', 'category_name' => $category));
?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
  <div class="col-12 col-sm-3 col-md-3 px-3 mb-5">
    <div class="product-box border p-3">
      <h3><?php the_title(); ?></h3>
      <p class="small"><?php the_field('brief_description'); ?></p>
      <a href="#" class="btn btn-success btn-block btn-sm">Option 1</a>
      <a href="#" class="btn btn-success btn-block btn-sm">Option 2</a>
      <a href="#" class="btn btn-success btn-block btn-sm">Option 3</a>
    </div>
  </div>
<?php endwhile; ?>