Support

Account

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

Solving

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 = '<ul>';
    		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 .= '<li>
    				<a>ID) . '">' . $p->post_title . '</a><br>
    				test = ' . get_post_meta($p->ID,"link",true) . '<br>
    			';
    
    			if( $file ) {
    				// NEVER RUNS
    				$output .= '
    					' . $file['url'] . '<br>
    					' . $file['filename'] . '<br>
    				';
    			}
    		}
    		$output .= '</ul>';
    		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

  • I had the same problem. I used var_dump to see what ACF was returning and it was just a numerical string. So then I used a WordPress function wp_get_attachment_url($my_variable) and that returned the correct URL.

  • In your loop you must supply the post ID to get fields from the specific post

    
    foreach ($my_posts as $p) {
      $file = get_field('link', $p->ID);
      ...
    

    get_post_meta() returns an number because ACF only stores the ID of the file attachment.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘ACF file query returning null or a number’ is closed to new replies.