Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI have a relationship field that outputs posts from a custom post type. However, if I unpublish one of those posts, it still shows in the outputted code.
I'm aware it's possible to set the fields to only allow the user to select from published posts only (in the admin), but I'm not sure how to add a check in the ACF output code to see if the posts are published or not.
The most likely situation where this will occur is when the client has been using the site for a while and using the relationship fields, but then deletes or unpublishes one of the posts and forgets to manually remove them from the relationship field.
Is there a way to check this on the fly?
Code below:
<?php
$holidays = get_sub_field('holidays');
if( $holidays ): ?>
<?php foreach( $holidays as $holiday): ?>
<?php setup_postdata($holiday); ?>
<a href="<?php echo get_permalink( $holiday->ID ); ?>">
<?php echo get_the_title( $holiday->ID ); ?>
</a>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
Closed. This question is off-topic. It is not currently accepting answers.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI have a relationship field that outputs posts from a custom post type. However, if I unpublish one of those posts, it still shows in the outputted code.
I'm aware it's possible to set the fields to only allow the user to select from published posts only (in the admin), but I'm not sure how to add a check in the ACF output code to see if the posts are published or not.
The most likely situation where this will occur is when the client has been using the site for a while and using the relationship fields, but then deletes or unpublishes one of the posts and forgets to manually remove them from the relationship field.
Is there a way to check this on the fly?
Code below:
<?php
$holidays = get_sub_field('holidays');
if( $holidays ): ?>
<?php foreach( $holidays as $holiday): ?>
<?php setup_postdata($holiday); ?>
<a href="<?php echo get_permalink( $holiday->ID ); ?>">
<?php echo get_the_title( $holiday->ID ); ?>
</a>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
Share
Improve this question
asked Dec 7, 2018 at 12:40
XavXav
3991 gold badge9 silver badges23 bronze badges
2 Answers
Reset to default 1Use get_post_status
to check the status of a post.
if( get_post_status( $holiday->ID ) == 'publish' ){
echo 'This is a published post';
}
Also note that your use of setup_postdata
is incorrect here. It is required to pass the global $post
var to the function, otherwise any template function that relies on the global $post
will output incorrect data. That said, you aren't using any of those functions, as you are using versions that allow passing of an explicit post ID, so you can just remove setup_postdata
as well as wp_reset_postdata
.
You could also just use ACF's filters for this. Put the following code in your functions.php.
// hide drafts
function relationship_options_filter($options, $field, $the_post) {
$options[‘post_status’] = array(‘publish’);
return $options;
}
add_filter(‘acf/fields/post_object/query/name=<YOUR FIELD NAME>’, ‘relationship_options_filter’, 10, 3);
more info directly from their site: https://www.advancedcustomfields/resources/acf-fields-post_object-query/