When creating a shop with Easy Digital Downloads, a client may want to have the featured feature. In this tutorial, you’ll learn how to create featured downloads for Easy Digital Downloads.
So, how shall we do that? We will have to:
- Create a metabox field (checkbox)
- Save the field
- Edit the Main Query
- Add a new class
Creating the Featured Downloads Field
To create a featured field, we may want to create a new metabox. That would be the straightforward approach since that can be done on any post type. But, since EDD is so well coded using the WordPress Plugin API, we don’t need to do that. We will just hook our field HTML into an already existing one.
For the metabox Download Settings, there is a hook edd_meta_box_settings_fields
. We will use that hook to add our field.
To create the field, we will use the EDD API. Our field will be a simple checkbox that will be true
or false
. Let’s now add the HTML.
The field will be saved as a postmeta of our product and the key of it will be _edd_featured
. If there is a postmeta, the variable $featured
will be true
and the checkbox will be checked. Otherwise, if we decide to uncheck it, EDD will delete that postmeta.
Saving the Featured Field
We could also use the hook save_post
to save our field but we would have to do several checks. All this can be bypassed with the EDD filter edd_metabox_fields_save
.
By hooking into that filter, we are adding our own meta key. Since the attribute name
of our checkbox is of the same value, EDD will be able to save it without our interference.
Editing the Main Query with Featured Downloads
To edit the query, we will have to hook into the action pre_get_posts
. We will have to add our code only if we are in the main query and if we are on the archive page of the Downloads.
We want our featured downloads to be displayed first, but we also want to display other downloads. Since the featured meta key is deleted if it’s false
, we will need to include downloads with and without that meta key.
Here we are adding a meta query where we want all of the downloads to be included. With the compare
set to EXISTS
and NOT EXISTS
we are sure that all downloads will be seen. With relation
set to OR
, we are including both of scenarios.
We also need to order the downloads. We will set the order so that the featured downloads show first. Others will use the default ordering: publishing date (date of creation).
As you can see, we are also using the key that we have set in our meta queries to set the ordering.
Adding the Featured Class
When we have featured products, we would like to apply a different style to them. That can be something simple such as a border, a different background color or something else. By doing that, we should add a class that would define if the product is a featured one or not.
We will hook into the filter post_class
to include that class.
With the variable $post
we can check if the post type is a download
. We will also check if that download is featured. If it is, we will add the class.
Conclusion
In this tutorial, we have created a simple but effective feature to our shop powered by Easy Digital Downloads. By using the filters and hooks provided by EDD, we can easily add anything we want.
If you are interested in learning more about developing with Easy Digital Downloads, I have an eBook for you. In this eBook, Easy Digital Downloads for Developers, I’ll cover more complex and challenging projects.
Become a Sponsor
Hey. I would like to add to the specific downloads a new class for editing css via the Meta Key Toggle in EDD Submission Form. I don’t know anything about it. Could you help me?
More specifically, I would like the user by selecting Toggle with the Meta Key assigned, could edit the css for blurring content 18+