Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active November 26, 2020 10:17
Show Gist options
  • Select an option

  • Save morgyface/b9c106509d6ba79195c07569466ef724 to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/b9c106509d6ba79195c07569466ef724 to your computer and use it in GitHub Desktop.
WordPress | ACF | Selected or fallback
<?php
/**
* Works with the ACF relationship field to always display the quota of posts
*/
function selected_or_fallback( $current_id, $total_required, $acf_relationship_name, $post_type = null) {
$excluded = array( $current_id );
$obtained = 0;
$related = array();
// Check to see if any posts have been selected via ACF relationship
$selected = get_field( $acf_relationship_name );
if( $selected ) {
$related = $selected;
$obtained = count( $selected );
if ( $obtained < $total_required ) {
// If the quota not met update excluded array for get_posts query
$selected_ids = get_field($acf_relationship_name, false, false); // Returns an array of IDs
$excluded = array_merge($selected_ids, $excluded);
}
}
if ( $obtained < $total_required ) {
if( $selected ) {
$post_type = get_post_type( $selected_ids[0] );
} else {
if ( !$post_type ) {
$post_type = get_post_type( $current_id );
}
}
$required = $total_required - $obtained;
$args = array(
'posts_per_page' => $required,
'post_type' => $post_type,
'exclude' => $excluded
);
$posts = get_posts( $args );
if( $posts ) {
$related = array_merge($related, $posts);
}
}
return $related;
}
@morgyface
Copy link
Author

morgyface commented Sep 7, 2019

Selected or fallback

Designed to be used with an ACF relationship field, typically on a post which gives the client the ability to select related posts.

This function defers to a get_posts query when the required quota has not been met, ensuring posts are returned even if the client has selected none or only some posts.

Typical use

$current_id = get_the_ID();
$related = selected_or_fallback( $current_id, '3', 'related_posts', 'posts');
if( !empty( $related ) ): ?>
    <ul>
        <?php foreach($related as $post): ?>
            <?php setup_postdata($post); ?>
            <li><?php the_title(); ?></li>
        <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); ?>
<?php endif; ?>

Other notes

I find the following ternary operator useful where the related posts section has a title.

echo ( $related_posts ? 'Related posts' : 'Recent posts');

@morgyface
Copy link
Author

Update May 2020

I noticed that if this was used on a page or a post type that didn't match the intended display of featured items, it would grab additional items based on the post-type of the current post/page. So, for example, if this was used on the homepage of the site and only 1 post was selected, the other two would be pages.

I've therefore adapted the gist so that it takes the post type from the first item selected as opposed to the post-type of the current id.

@morgyface
Copy link
Author

Update October 2020

Further update allowing you to pass in a post-type variable. Whilst the last update was good, it failed where no posts were selected on somewhere like the homepage as the function wouldn't be able to determine the intended post-type and would instead display pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment