Home › Forums › Backend Issues (wp-admin) › Post Object is showing random Posts, not the selected Post
Here is a link to my code: https://gist.github.com/jrstaatsiii/7736698
It seems to be correct based on this article: http://www.advancedcustomfields.com/resources/field-types/post-object/
However, for whatever reason, it seems to be showing a random post from the correct post type, but not post selected in the options panel. Any ideas why this might be happening?
Thanks! -Rich
Hi @jrstaatsiii
Can you debug the value like so:
<?php
echo '<pre>';
print_r( get_field('ssm_featured_course', 'options') );
echo '</pre>';
echo '<pre>';
print_r( get_field('ssm_featured_course', 'options', false) );
echo '</pre>';
die;
?>
The first print_r will display the post object returned, the second will display the DB value (post_id).
Can you post back the results? Do they look correct?
Thanks
E
WP_Post Object
(
[ID] => 143
[post_author] => 1
[post_date] => 2013-11-24 21:49:38
[post_date_gmt] => 2013-11-24 21:49:38
[post_content] =>
[post_title] => 1 Hour HTML
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => 1-hour-html
[to_ping] =>
[pinged] =>
[post_modified] => 2013-11-24 21:49:58
[post_modified_gmt] => 2013-11-24 21:49:58
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://chrispirillo.dev/?post_type=ssm_course&p=143
[menu_order] => 0
[post_type] => ssm_course
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
143
It looks correct here, and yet it still shows the wrong post, and for whatever reason, it shows a different post for each page this is used on.
Hi @jrstaatsiii
So just to confirm, the get_field returns the correct post every time (post_id = 143)?
If so, the issue must be with your code. What code are you using to render the post?
Thanks
E
I have no doubt the issue is with my code. Here is the gist:
https://gist.github.com/jrstaatsiii/7736698
Hi @jrstaatsiii
Thanks for the link. I can’t see any issues with your code, but perhaps the setup_postdata is not working correctly for some unknown reason.
Can you modify the code to not use the setup_postdata like so:
<?php if ( get_field('ssm_featured_course', 'options') ) { ?>
<?php $p = get_field('ssm_featured_course', 'options'); ?>
<div id="featured-course">
<div class="wrap">
<small>Featured Course</small>
<p class="faux-title"><a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a></p>
<?php if ( get_field('ssm_course_description', $p->ID) ) { ?>
<div class="course-description">
<?php the_field('ssm_course_description', $p->ID); ?>
</div>
<!-- end .course-description-->
<?php } ?>
<a class="button red" href="<?php echo get_permalink( $p->ID ); ?>">View Course</a>
</div>
<!-- end .wrap -->
</div>
<!-- end #featured-course -->
<?php } ?>
Does this work?
Thanks
E
yup that worked. not sure why setup_postdata isn’t, but thanks
The topic ‘Post Object is showing random Posts, not the selected Post’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.