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">';
          $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>';
        $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: Here is also the ACF Location Rules

ACF Location Rules

Custom Field Setup

Custom Field Setup

  • Welcome! Can you post another screenshot of the field settings? That way we can see how the field is configured as well. – Tom Jul 6 at 10:57
  • @Tom I added another screenshot for one of the two custom fields. The other is exactly the same only with different field label and field name. – Tes3awy Jul 6 at 11:05
0

I just tested it on Divi (3.25.4) and it should work, unless there's a bug related to shortcodes in 3.25.3.

Make sure the custom post type (Menu) you have created in CPT UI has Categories (WP Core) ticked in the Built-in taxonomies, and that some custom posts created in the Menu post type have Pizza as a category. Also, I hope in the Divi page's text module's text area you're writing the shortcode with the category name argument correctly? Like:

[RESTAURANT_MENU category_name="Pizza"]

  • The problem is with ACF not shortcodes, sir. Everything is working except for get_field() from ACF. – Tes3awy Jul 6 at 12:04
  • What is a var_dump of both get_field() calls returning for you? NULL? – sgr12 Jul 6 at 12:26
  • Yes it returns NULL – Tes3awy Jul 6 at 12:27
  • If post title, content, and post ID are showing correctly for you, you could also try get_post_meta($menu_product->ID,'regular_size_price', true) instead of calling get_field(). Explore the wp_posts and wp_postmeta tables and check that the custom field is correctly stored for the post_id with meta_key as regular_size_price and has the correct value you entered when creating/editing the custom post. – sgr12 Jul 6 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.