Creating a Featured Post Meta Box Checkbox

I needed a way to create a checkbox in WordPress that allowed me to set a post as a featured post and then to feature those posts on a page while still displaying other posts of the same category.  For example, I want to have page that pulls posts from the category of “tutorials” but I want some of those posts to be featured and always show on the top.

This took a bit more digging than I anticipated but I did finally come up with a solution with the help of this post from ThemeFoundation.com as well as utilizing the “orderby” with “meta_value” parameters from the Codex found here.  In the end, the challenge was getting posts to show up that were both featured and not featured on the same template and I ended up creating 2 loops to get it accomplished.

To get started, I created my metaboxes as a Plugin.  I like the plugin solution rather than adding to the functions.php file because 1) it doesn’t get overwritten during theme updates, etc. (although this can be mitigated with a child theme), 2) it allows me to turn it off quickly if something breaks, but 3) more importantly, it make the featured post option not theme dependent (even though the template that uses the feature is).

1) Creating the Plugin:

There are lots of posts out there about how to create a plugin including the article I did recently about how to create a Google Analytics Plugin from the reference source of wpninjas.com.  Basically, create a directory inside your “Plugins” subdirectory and then create a the plugin as a .php file and make sure your header info is included:

Then we add the functions we need to add the metabox functionality.  I have it commented below and start functions with the prefix “prfx_” as instructed for the reasons included in the Theme Foundation article.  You will notice 3 main commented sections:

  1. Add the metabox to the “Post Edit” screen
  2. Output the checkbox content of the meta (here you can add multiple boxes, text boxes, drop downs, etc. – See ThemeFoundation.)
  3. Save the meta output to the database.

Here is the final code that I ended up with:

Create the plugin, activate it and then visit your site in the post edit and see if you can find your new meta box:

featured meta box

2) Using the Meta Box Content In the Template

There are lots of ways that you can use this in the template.  You can display text, use it to add styles, call up specific content, etc.  For this, there is a lot of great info and examples in the Codex here – http://codex.wordpress.org/Class_Reference/WP_Query

For me, I wanted to display content from the same category (without duplicating) but sort all of the posts checked “featured” to the top.  So I created 2 loops (probably better ways out there to do this so if you know of one, please comment).

First, I created a loop to pull the content in for the featured posts (and I put it in its own <div> so I could style it later if I want to).  I did this using the “meta_query” function where you can do a lot of cool things like add and / or arguments, pull different items from content types, etc. For example, they reference using it to pull a custom field from a specific post type:

Single Custom Field Handling:

Display posts from a single custom field:

You will see that you have a “meta_key” and a “meta_value” – in this case the meta key is the “featured-checkbox” that we defined in the metabox output section.  The value is “yes” which we also defined there as well as in the “save” section.  For my case, I pulled one loop for the featured content at the top, then another loop for the non-featured content below.  Maybe not as elegant as it could be because I feel like there has to be a way for me to tell one loop to pull both and simply sort differently but I finally settled on an adequate solution as I could not find the exact that I was looking for and I am not a programmer…    In the loops, I pulled the post thumbnail, the excerpt, and then added a read more link.

Here is the final product: http://www.durangoreservations.org/durango-vacation-packages/.  You really can’t tell the difference between the featured and not at this point because I just wanted the most popular vacation packages at the top for the ‘ol 80/20 rule but I may style them differently later and left the door open for that.

Hope this helps others and, as always, back up, proceed at your own risk, and good luck out there…