Search

Programming
Programming

We provide developers and engineers the guides and tutorials they...

Content
Content

Content is king. It's not just about how you're going to write, b...

Events
Events

Looking for an engaging connection with thousands of other people...

Feature Posts for WordPress with ACF

Feature Posts for WordPress with ACF

Featured posts let you choose specific posts to highlight on your webpage. There are several plugins available to achieve featured posts as well.

In this article we'll use a pretty simple approach to add featured posts using Advance Custom Fields Plugin.

Create a new Custom Field

Head over to the custom fields page and a new custom field, let's name is Featured Post and set the field type to true/false, set the location to Post & simply publish it.

You can now see a simple check-box on each of your post page. Now let's add some functionality to make it work.

Featured Posts Query

In your index.php or either in your functions.php, however your theme structure is for your Wordpress website, add the following code to get the featured posts.

$featuredPosts = get_posts( array(
'posts_per_page' => 4,
'orderBy' => 'desc',
'meta_query' => array(
array(
'key' => 'featured_post',
'compare' => '=',
'value' => '1'
)
)
) );

You can then pass the featured posts and loop through these to display on your frontend however you want. As simple as it is.

Final Words

Now to make any of your post a featured post, just check the box next to featured custom field and well that's it. Simple enough, eh?

Usama Muneer

About author

A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

View all posts (110)
Your experience on this site will be improved by allowing cookies Cookie Policy