Support

Account

Home Forums Front-end Issues CPT Loop not returning get_field but does get_post_meta

Solved

CPT Loop not returning get_field but does get_post_meta

  • I love how quickly I was able to use ACF to go from content idea to data capture on admin screen to (almost) output on front-end. This is a fantastic plugin!

    My one small current issue has to do with a CPT query loop that returns contetn via the get_post_meta tag for fields but not via either get_field or the_field. I’m intending to have this as a function call or possibly shortcode within a plugin which is why I’m not echo’ing out directly.

    
    global $post;
    $highlights = new WP_Query();
    $highlights->query('post_type=highlight&posts_per_page=-1');
    
    if ( $highlights->have_posts() ) : 
    	$output = "<div id='highlights'><ul>";
    	while ( $highlights->have_posts() ) : $highlights->the_post(); 
    	setup_postdata($post);
            	 $output .= "<li>";
    	 $output .= "<strong>Title: " . get_the_title() . "</strong><br/>";
    	 $output .= "<strong>ID: " . $post->ID . "</strong><br/><br/>";
     
    	 $output .= "get_post_meta CTA: " . get_post_meta($post->ID, "highlight_call_to_action", true) . "<br/>";
    	 $output .= "get_post_meta Link: " . get_post_meta($post->ID, "highlight_link", true) . "<br/>";
    	 $output .= "get_post_meta Image: " . get_post_meta($post->ID, "highlight_image", true) . "<br/>";
    	 
    	 $output .= "<br/>";
    	 $output .= "get_field CTA: " . get_field("highlight_call_to_action") . "<br/>";
    	 $output .= "get_field Link: " . get_field("highlight_link") . "<br/>";
    	 $output .= "get_field Image: " . get_field("highlight_image") . "<br/>";
    	 $output .= "</li>";
    	
    
    	endwhile; 
    	$output .= "</ul>";	
    endif; 
    
    wp_reset_query();
    

    And the ACF setup which I exported from admin console generation:

    
    register_field_group(array (
    'id' => 'acf_highlight-details',
    'title' => 'Highlight Details',
    'fields' => array (
    	array (
    		'key' => 'field_52837b715ccfd',
    		'label' => __('Call To Action'),
    		'name' => 'highlight_call_to_action',
    		'type' => 'text',
    		'required' => 1,
    		'default_value' => '',
    		'placeholder' => 'The secondary text that appears within the highlight',
    		'prepend' => '',
    		'append' => '',
    		'formatting' => 'html',
    		'maxlength' => '',
    	),
    	array (
    		'key' => 'field_52837c11e2327',
    		'label' => __('Link'),
    		'name' => 'highlight_link',
    		'type' => 'page_link',
    		'instructions' => __('Where should the user go when clicking on the highlight?'),
    		'required' => 1,
    		'post_type' => array (
    			0 => 'post',
    			1 => 'page',
    			2 => 'highlight',
    		),
    		'allow_null' => 0,
    		'multiple' => 0,
    	),
    	array (
    		'key' => 'field_52837cfa9e9ad',
    		'label' => __('Image'),
    		'name' => 'highlight_image',
    		'type' => 'image',
    		'required' => 1,
    		'save_format' => 'id',
    		'preview_size' => 'medium',
    		'library' => 'all',
    	),
    ),
    'location' => array (
    	array (
    		array (
    			'param' => 'post_type',
    			'operator' => '==',
    			'value' => 'highlight',
    			'order_no' => 0,
    			'group_no' => 0,
    		),
    	),
    ),
    'options' => array (
    	'position' => 'normal',
    	'layout' => 'default',
    	'hide_on_screen' => array (
    	),
    ),
    'menu_order' => 0,
    			));
    
  • Hi @Idealien

    I see 2 issues with your above code:
    1. Your post_meta function use the $post->ID, however your get_field does not
    2. Your setup_postdata() function may be causing the issue.

    Does your line work correctly:

    
    $output .= "<strong>ID: " . $post->ID . "</strong><br/><br/>";
    

    Can you also read over the WP docs to see some code examples of loops. Perhaps the get_posts function will be clearner?

    Thanks
    E

  • $post->ID does return value in the previous code yes. I tried with and without $post->ID appended to get_field call.

    This page has a good summation of why I was using a separate WP_Query class object – http://www.binarymoon.co.uk/2010/03/5-wordpress-queryposts-tips/

    Using a get_posts approach did not change the result but included for future reference anyone else – based closely off the example on get_posts template tag codex page.

    
    $args = array( 	'post_type' => 'highlight', 
    	'posts_per_page'=> $count, 
    );
    
    $highlights = get_posts( $args );
    		
    if (count($highlights)) {
    			
    	$output = "<div id='highlights'><ul>";
    			
    	foreach ( $highlights as $post ) : setup_postdata( $post );
    
    		$output .= "<li>";
    $output .= get_field('highlight_call_to_action', $post->ID) . "<br/>";
    $output .= get_field('highlight_call_to_action') . "<br/>";
    $output .= get_post_meta($post->ID, "highlight_call_to_action", true) . "<br/>";
    			
    		$output .= "</li>";
    			
    	endforeach; 
    			
    	$output .= "</ul>";	
    
    	wp_reset_postdata();
    			
    }
    

    In either loop format case using get_field_object with / without the preceeding _ returns a blank shell response. Whether $post->ID is appended or not. Glad to have a temporary working solution, but would obviously prefer to make use of the ACF output fields as I get into more advanced uses with it.

  • Even doing the following direct call to a post ID that I know has data in my page template doesn’t work.

    <?php echo get_field(‘highlight_call_to_action’, 64); ?>

    Might it be related to the fact I’m using it assigned to a custom post type?

  • Hi @Idealien

    Thanks for posting your last snippet of code. This shows that the issue is not related to your loop at all, there is something else going on.

    Can you please debug the returned value like so:

    
    <?php 
    
    echo '<pre>';
    	var_dump(get_field('highlight_call_to_action', 64) );
    echo '</pre>';
    die;
    
    ?>
    

    What result do you get?

    Can you also check your DB and make sure that for post_id = 64, there is a row in your wp_postmeta table which contains the highlight_call_to_action value?
    What does it look like? Does it also have a ‘reference value’ below it in the table (underscore version)? Screenshot?

    Thanks
    E

  • The haystack gets a little smaller ;P

    Either inside a loop or outside I run that snippet and get “bool(false)” as result with the admin bar and rest of page below where I put the snippet not displaying. If I remove the die; I get rest of page but same result.

    All results from postid = 64 from postmeta table are screenshot to http://imgur.com/vKiw951 – It does not have a reference value in the table though the rest of the meta fields useful data is present.

  • Hi @Idealien

    Thanks for the screenshot, this shows that the data is there along with the reference values.

    I’m not sure why your code is not returning any data. Lets now look at 2 other factors:

    1. In which file are you running the get_field code?
    2. Is the post with ID = 64, a published post? Or is it an autosave / draft? You can see this information in the wp_posts table?

    Are you able to load ACF data anywhere else in the site? Is it just this one post, or is it completely not working?

    The issue may turn out to be a conflict with another plugin? You can check this by creating a new blank WP site with just ACF, and recreate the fields / get_field code to test it out.

    Hope that helps

    Thanks
    E

    1. header.php, page-templates/front-page.php and archive-{post-type}.php all exhibit the same behaviour. As well as within a plugin function call.
    2. 64 is a published post.

    With all plugins except ACF deactivated I’ve also tried a very simple ACF field group attached to the standard post and get similar results when placing the get_field call in header.php.

    Will create a fresh site shortly and follow-up if that does provide any difference in output.

  • Changed my local dev environment from VVV to a more generic lamp vagrant setup and the issue has gone away. Not sure what the direct cause in that mix is but happy that it is no longer affecting me.

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

The topic ‘CPT Loop not returning get_field but does get_post_meta’ is closed to new replies.