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?