As I was browsing the StudioPress forums recently I came across a question about adding a new “order by” option to the Genesis Featured Posts widget. The person wanted to be able to display posts using the date the post was last modified, which is something I’ve also done before and found useful.
The last time I did it though, I used a custom widget rather than the Genesis one, so I made a note to take a closer look at doing it with the Genesis widget when I had time.
If you’re going to be doing this along with me, I don’t recommend making any changes to the core Genesis code so the first thing to do is make a copy of the widget code. We can either copy the file at
\genesis\lib\widgets\featured-post-widget.php and drop that into our child theme folder, or open the file and copy the contents into our child theme functions.php file.
There’s quite a lot of code in the file so I think we should go for copying the file itself. To keep things nice and tidy we should also put it in a sub folder called lib. Once we’ve done that we’ll also need to include it in our child theme functions.php file by adding the following code;
Next, we’ll need to change the name of the widget class so there is no conflict between our customised widget and the original widget. If we open the file we copied, we’ll see that the original widget class is called “Genesis_Featured_Post”. Let’s call ours “Genesis_Child_Featured_Post”.
Lastly, we’ll need to unregister the original widget and register our version. We can do that by adding the following code to our child theme functions.php file;
Now we have the original Featured Posts widget replaced by our custom version, we can start making our changes.
In the default Genesis widget there is a section of code which spits out the dropdown list for the sort by parameters. It looks like this;
You can see that the option values for the dropdown list are hard coded.
The last time I did this, I simply added the new order by parameter. You would have seen something like this, with an extra option added to the list;
That would work fine, but what if we could make it even better? Suppose we made the order by parameters an array which could be changed using a standard WordPress filter? If we ever decided to limit or extend the order by parameters in the future it would be a simple case of adding a function.
So let’s do that. We’ll take the standard list of order by parameters along with our “modified” one and dropped them into an array. We’ll then add a filter to the array which is applied before it is used to create the options for the dropdown list. Here’s what it looks like;
Now, if we ever want to change the list of order by parameters now, all we do is add a function to the “gct_featured_posts_widget_orderby” filter like so;
That’s pretty much it really. If you have any questions or suggestions, don’t hesitate to leave a comment!