Support

Account

Home Forums General Issues Post Object

Helping

Post Object

  • Hello,

    I’m having problems with the Post Object field and trying to get that to display on the page. I’ve looked at the page for it, but can not even get the simple debugging code to work via code below, nothing happens.

    <div class="col-xs-12 col-sm-12 col-md-12 page-style">
    
    <?php 
    
    	echo '<pre>';
    	    print_r( get_field('featured_property')  );
    	echo '</pre>';
    	die;
    
    ?>
    
    </div>

    Same thing with the ‘Display data for a single/multiple post object’ code.

    $post_object = get_field('featured_property');
    
    if( $post_object ): 
    
    	// override $post
    	$post = $post_object;
    	setup_postdata( $post ); 
    
    	?>
        <div>
        	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        	<span>Post Object Custom Field: <?php the_field('property_details_excerpt'); ?></span>
        </div>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

    Am I missing something?

  • Hi naz,

    A few possibilities. One, is the Relational Post Object field set to output the Post ID or the Post Object? Just figured I’d ask in case it’s an easy fix, as it should be Post Object by the way you are using it 🙂

    Second, retrieval by field name will only work if a value for that field has already been saved to th post/page in question, so that field-name<->field-key lookup data has been populated. Check out get_field_object() documentation, under the heading field_name vs field_key. It may not be readily apparent from the documentation, but for all functions that take a field name, you may (and really should) specify a field key. You may view the field keys of your fields by selecting Field Keys in the Screen Options of the Field Group add/edit screens.

    Third, not specifying a post_id will only work if there is a current post setup. Is this code being executed within The Loop? Remember that even pages have a loop; it retrieves a single “post”. If not, you might want to specify the second parameter to get_field() ($post_id) or move your call to within The Loop.

    ————————

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

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

The topic ‘Post Object’ is closed to new replies.