Roots Discourse

Loop Featured Posts with ACF Field

I am currently looping through all my wordpress posts in my index.blade.php file with this:

@while (have_posts()) @php the_post() @endphp
    @include('partials.content-'.get_post_type())
@endwhile

And then in my content-post.blade I echo them with this:

<h2 class="h4">{!! the_title() !!}</h2>

This works fine. However I have created an ACF field ‘is_featured’ and I’d like to create a new loop that loops only those posts that are set to is_featured = true at the top of my page. Being new to sage I’m not sure how to proceed. I have tried using filters but it seems like that would be unnecessary.

Could be that all you need is to use Sticky posts (Sticky Posts | WordPress.org) instead of your ACF-field. This should automatically display the sticky posts first in the loop.

If you want to separate the featured posts from the rest, maybe fetch the featured posts using a meta query on the ACF value (ACF | Query posts by custom fields). Or you could still use sticky posts instead of an ACF field since you can fetch posts based on its ”sticky status”.

How you would send the data to the view-file in the second example depends on which version of Sage you are running and on how your controllers/components are set up.