0

I am using ACF and CPT cooperatively. I created a shortcode to be placed in a text module in my theme. It works well. Yet, when I call ACF get_field(), it's not returning any value. I tried looking into this question and also this one, but neither works.

I double-checked ACF fields name. Also tried to change the input type from text to number but still no hope.

Development Environment

  • WordPress Version: 5.2.2 (Latest at the moment)
  • Theme/Child Theme Version: Divi 3.25.3

The shortcode I created:

  <?php
    add_shortcode('RESTAURANT_MENU', 'fetch_menu_products');
    function fetch_menu_products($atts)
    {
      $atts = shortcode_atts(array(
        'category_name' => ''
      ), $atts);
      $category_name = $atts['category_name'];

      $args = array(
        'category_name' => $category_name,
        'post_type' => 'menu',
        'numberposts' => -1,
        'post_status' => 'publish'
      );

      $output = '';
      $menu_products = get_posts($args);
      foreach ($menu_products as $menu_product) {
        setup_postdata($menu_product);
        $output .= '<section class="menu-item-wrapper">';
        $output .= '<h3 class="menu-item__title">' . $menu_product->post_title . '</h3>';
        $output .= '<div class="menu-item">';
        $output .= '<div class="menu-item-description">';
        $output .= '<p class="menu-item-description__text">' . $menu_product->post_content . '</p>';
        $output .= '</div>';
        $output .= '<ul class="menu-prices-list">';
        if (get_field("regular_size_price") || get_field("large_size_price")) {
          $output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID)  . ' Currency</li>';
          $output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
        }
        if (get_field("price")) {
          $output .= '<li class="menu-prices-list--item">' . get_field("price", $menu_product->ID)  . ' Currency</li>';
        }
        $output .= '</ul>';
        $output .= '</div>';
        $output .= '</section>';
      }
      wp_reset_postdata();
      return $output;
    }

Can any help me find out why isn't it returning any value, please? Thank you.

Update: The ACF Location Rules

ACF Location Rules

Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • 1
    Can you try get_post_meta($menu_product->ID, 'regular_size_price'); and see what is returning? https://developer.wordpress.org/reference/functions/get_post_meta/ – NewUser Jul 06 '19 at 10:50
  • @NewUser it returns nothing. – Tes3awy Jul 06 '19 at 10:56
  • @NewUser I managed to use your method `get_post_meta($menu_product->ID, 'regular_size_price')[0]` and neglect using `get_field()` function provided by ACF. – Tes3awy Jul 06 '19 at 11:30
  • Yes I have came through this situation many times. For some reasons get_field doesn't work sometimes. So rather than using get_field() most of the times I use get_post_meta() – NewUser Jul 06 '19 at 11:46
  • @NewUser Thanks a lot, sir. Can you please add your answer so I can accept it? – Tes3awy Jul 06 '19 at 12:09
  • I have added my answer. Please check it. – NewUser Jul 06 '19 at 12:20
  • @Tes3awy The function that @NewUser asked you try should have had an extra parameter in it (so it won't be returned as an array, then you can easily just echo it). You should change this to `echo get_post_meta($menu_product->ID, 'regular_size_price', true);` instead. – Lee Nov 17 '21 at 08:42

2 Answers2

1

Try to use get_post_meta() instead of get_field(). As you are already using ACF then use this code to get value from post meta

get_post_meta($menu_product->ID, 'regular_size_price')[0]

For more help on get_post_meta() you can check this link

NewUser
  • 12,713
  • 39
  • 142
  • 236
0

If my assumption is correct, your li elements are not being printed when you return the $output variable. That should be the case since you are using get_field function outside of theloop without passing post id in if statement.

Below is the corrected code:

if (get_field("regular_size_price", $menu_product->ID) || get_field("large_size_price", $menu_product->ID)) {
    $output .= '<li class="menu-prices-list--item">R ' . get_field("regular_size_price", $menu_product->ID)  . ' Currency</li>';
    $output .= '<li class="menu-prices-list--item">L ' . get_field("large_size_price", $menu_product->ID) . ' Currency</li>';
}

I hope this helps.

Faham Shaikh
  • 983
  • 2
  • 6
  • 15
  • The problem is not in the `if` condition. If I remove the the `if`, I get `R empty Currency`. – Tes3awy Jul 06 '19 at 10:19
  • @Tes3awy I see that you managed to get the data using `get_post_meta` and I am quite surprised that this didn't work with `get_field`. Just for the purpose of better understanding the underlying issue can you do `var_dump(get_field("regular_size_price", $menu_product->ID))` and post the results. I am under the impression that the data is not being saved in proper format somehow. – Faham Shaikh Jul 06 '19 at 12:09
  • Using `var_dump` prints `NULL` which is very weird because all the fields are populated! – Tes3awy Jul 06 '19 at 12:12
  • If you enable debug log in config, you might find more about the issue. There is a possibility that your installation environment is not supported as was the case with [this one](https://support.advancedcustomfields.com/forums/topic/cpt-loop-not-returning-get_field-but-does-get_post_meta/) – Faham Shaikh Jul 06 '19 at 13:25
  • I tried to do a clean install, and install only ACF, It doesn't work. I am suspecting that it has something to do with Divi itself. Yet, the `get_post_meta` works well with the current installation. So I'll go with it for now. – Tes3awy Jul 06 '19 at 13:57
  • Well getting things to work should be the first priority so wise choice there. Trying to identify the problem helps you handle similar issue in future so in free time trying to fix this and sharing the solution will be a help to the community. – Faham Shaikh Jul 06 '19 at 14:14
  • 1
    One more thing, `setup_postdata` requires the passed variable name to be `$post`. So it should be `global $post; $post = $menu_product; setup_postdata($post);`. That way you can directly use `the_title` and other core functions. – Faham Shaikh Jul 06 '19 at 14:20
  • Thanks a lot, sir. And sure once I figure out the right solution; not the easy one; I'll share it (Y). – Tes3awy Jul 07 '19 at 13:40
  • Good luck mate!! – Faham Shaikh Jul 07 '19 at 13:53