Support

Account

Home Forums ACF PRO ACF file query returning null or a number

Unread

ACF file query returning null or a number

  • Hi I just purchased ACF pro and am having an issue with an ACF file type associated to a custom post type called “link”, it either returns null or a number which I assume is the ID but not what id in the documentation of usage for $file[“url”] as get_field(“link”); is always null. Full details below.

    I’ve setup a custom post type called “links”, singular “link” via:

    function setup_links_post_type()
    {

    $args = array(
    “labels” => array(
    “name” => “Links”,
    “singular_name” => “Link”,
    ),
    “menu_position” => 2,
    “hirarchical” => true, // page vs posts mode
    “public” => true,
    “has_archive” => true,
    “supports” => array(
    “title”,
    “editor”,
    “thumbnail”,
    ),
    “menu_icon” => “dashicons-admin-links”,
    // “rewrite” => array(“slug” => “downloads”)
    );

    register_post_type(“links”, $args);
    }

    add_action(“init”, “setup_links_post_type”);

    Then I used ACF Pro to setup a field group called ACF1, then in this group I created a custom field called “link” which is an ACF type of “file” and attached it to the custom post type of “link”.

    I use the following code to do a query:

    $args = array(
    ‘numberposts’ => 3,
    ‘post_type’ => ‘links’,
    );
    $my_posts = get_posts($args);

    if (!empty($my_posts)) {
    $output = ‘

      ‘;
      foreach ($my_posts as $p) {
      $file = get_field(‘link’);
      var_dump( get_field(‘file’) ); // dumps NULL
      // print_r( get_post_meta($p->ID,”link”,true) ); // This will print out a number

      $output .= ‘

    • ID) . ‘”>’ . $p->post_title . ‘<br>
      test = ‘ . get_post_meta($p->ID,”link”,true) . ‘<br>
      ‘;

      if( $file ) {
      // NEVER RUNS
      $output .= ‘
      ‘ . $file[‘url’] . ‘<br>
      ‘ . $file[‘filename’] . ‘<br>
      ‘;
      }
      }
      $output .= ‘

    ‘;
    echo($output);
    }

    I always get

    $file = get_field(‘link’); // NULL

    and a number from

    get_post_meta($p->ID,”link”,true);

    Anyone know why or got a working script to get the file name of the file link from ACF (Advanced Custom Fields)?

    Thanks

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.