$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ACF relationship field outputting unpublished content|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ACF relationship field outputting unpublished content

matteradmin9PV0评论
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 question

I 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 question

I 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
Add a comment  | 

2 Answers 2

Reset to default 1

Use 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/

Post a comment

comment list (0)

  1. No comments so far