WP_Query: Get Posts by Relative Date
Posted: April 11, 2013 Filed under: WordPress Development Leave a comment »Someone asked me today how to use WP_Query to get posts by a relative date. The codex provides an example on how to achieve this. Heres what is shown on the codex.
I feel that its sloppy and not very flexible. Having to add the filter and remove it each time you want to do this is ugly. I solved this some time ago and had to dig through some old code to find it.
Ultimately what I have done is the same solution, using the same filter, and utilizing strtotime() to get the relative date. However i’ve wrapped it into a simple “since” parameter that can be used on any query. Just a little nicer and simpler to use.
This technique can easily be adapted to other custom parameters which result in modifications to the where or join filters.
Better Git: Interact With a Branch, Without Checking it Out.
Posted: April 10, 2013 Filed under: Better Git Leave a comment »Tricks for interacting with a branch, without checking it out.
Browse a directory (like ls):
Syntax: git show [ref]:[path]
> git show master:your/path/
See contents of a file (command as above):
Syntax: git show [ref]:[filepath]
> git show master:your/path/file.php
Checkout a specific file or directory from a different branch:
Syntax: git checkout [ref] -- [path]
> git checkout master -- your/path/file.php
Note: There are other ways to do similar tasks such as `git ls-tree` and they may have more options. However I find these to be more accessible and easy to remember.
Better Git: git fetch not getting tags
Posted: February 21, 2013 Filed under: Better Git Leave a comment »I just ran into a little gotcha with regard how fetch handles tags. When it pulls down commits, you will usually see that it also pulls down tags. I was a little confused today when fetch was refusing to pull a tag that was clearly in the repository (“git ls-remote --tags“) lets you see tags available on the remote).
I kept running the fetch command but the tag wouldn’t get pulled down. The reason has to do with the way fetch works, it only fetches tags that are direct references to a commit that is in a branch being fetched. To get all tags regardless of what commits they reference use the fetch in the following way.
From the git manual (“git help fetch”):
-t, –tags Most of the tags are fetched automatically as branch heads are downloaded, but tags that do not point at objects reachable from the branch heads that are being tracked will not be fetched by this mechanism. This flag lets all tags and their associated objects be downloaded. The default behavior for a remote may be specified with the remote.<name>.tagopt setting. See git-config(1).
This will effectively do the inverse of normal fetch. It will fetch all tags, and bring with is any necessary commits.
Hope this helps some poor confused folks out there a little bit of greif.
Better Git: How to find out what submodules there are in a different branch
Posted: February 5, 2013 Filed under: Better Git Leave a comment »Generally speaking Git is fantastic and easy to use – one of the few pain points is where submodules butt heads with branches.
Far from solving all the problems in related to this – I’ve found an easy way to find out what submodules are in a branch, commit, or tag without having to check that reference out. This method also shows you the commits that each submodule is currently pointing to.
The ls-tree command is fairly simple, its like running the ls unix command but includes git information – like the type of object each item is and the hash reference for that object. That command can also be used on a specific directory also by using <reference>:<path>. The -r flag makes it recursive so that is searches through all the subdirectories in the branch.
The second part of this is a simple grep command, that filters the results by those of type “commit”, which is how ls-tree represents submodules. I’ve included a pattern to match the word commit followed by the SHA1 hash – to avoid getting items that have the word commit as part of a folder or filename.
Hope this helps someone out there – it took me a while to figure out how to do this.
Incorrect Datetime Bug Fix Updated – Version 1.1 Released
Posted: January 7, 2013 Filed under: Plugins Leave a comment »A user of the Incorrect Datetime Bug Fix plugin submitted a bug report last week. While the plugin was turned on the user was getting an error about improper usage of wpdb::prepare() in WordPress 3.5.
Warning: Missing argument 2 for wpdb::prepare()
This threw me for a loop, since its such a simple plugin that I’ve never had to change, I assumed that something changed in the new version of WordPress 3.5. I was all ready to submit a new trac ticket. As it turns out, it was my usage of wpdb::prepare() that had always been incorrect, if you are a developer and are interested in knowing more about the problem, read Andrew Nacin’s article on the subject. In the past WordPress simply wasn’t throwing out an error and that’s what was changed in WordPress 3.5. I found this by using git’s “bisect” feature, which is really great and I might write a blog post about it in the future.
As always I greatly appreciate but reports, idea submissions for my plugins, and any other feedback regarding the plugins I maintain. It is whats great about Open Source and it helps me validate its usage in the corporate world. The new version is now available for download. Please update to avoid these errors from being reported.
Media Categories 1.5 Released
Posted: December 26, 2012 Filed under: Plugins 4 Comments »Last week I finally released Media Categories 1.5, but I neglected to write a post about it. Most of the UI issues have been resolved with the Media Modal, however due tot time constraints I could only ensure the plugins compatibility as far back as WordPress 3.3. It doesn’t break the site earlier than that, but selected taxonomy terms dont save properly. As of now, I have no plans to extend support to 3.2.x or before.
I do intend however on adding a few more features that I think are missing from WordPress 3.5 as soon as I have the time. The most obvious of which I will also try to get committed to core. Said enhancements include:
- Fixing the counter on the right of the attachment table – its supposed to indicate the number of media items per category but it does not count accurately.
- Adding a taxonomy filter to the attachments page – for most post types with a taxonomy enabled, the post types admin page shows a ‘View all categories’ drop down. This is missing from media in WordPress 3.5. I would also try to work some similar filtering ability into the Media Modal.
- There seem to still be some UI issues in the Media Modal when multiple taxonomies are assigned to a post type.
- I may create an admin interface in the settings area to allow admins to simply check from a list of checkboxes, which taxonomies (of all available taxonomies) they would like to enable for media.
- I may also include some simple functions for retrieving attachments by taxonomy. Although these would essentially be a simple wrappers of WP_Query or one of its wrapper functions, I believe it would serve as an example to developers on how to get started.
Take a look at how great attachments with taxonomies look in WordPress 3.5 by downloading Media Categories 1.5. Look out for some of these improvements I mentioned above in the next version, Media Categories 1.6.
One additional note: At some point in the near future I will update the plugins pages on this site,… I promise. However, please understand that the pages on this site are meant to contain the same information as the WordPress plugin repository does, mostly in the ‘Other Notes’ tab.
Media Categories 1.5 Beta 2
Posted: December 15, 2012 Filed under: Plugins 1 Comment »I have solved the UI problem in the modal that was holding up the release of Media Categories 1.5. However due to an additional bug reported to me on GitHub Issues, I did not get a chance to test the plugin in earlier version of WordPress.
The bug reported was due to an oversight on my part – in my rush to get this out the door, I had hardcoded much of the Javascript and CSS to work specifically for the ‘category’ taxonomy which I meant to go back later and fix – but I forgot. The Media Categories plugin has, since its inception, been intended to be used with any desired taxonomy. Thanks to GitHub user “stvwlf” for the bug report.
I believe Media Categories 1.5 should be fully functional for WordPress 3.5 at this point, but I wont release until I get a chance to test all aspects of it are backward compatible. If you would like to download Media Categories 1.5 Beta 2 ( zip / tar.gz ) now you can get by downloading the version specific tag in GitHub or you can get whatever is the most recent right off the master branch.
Media Categories 1.5 Beta and WordPress 3.5
Posted: December 13, 2012 Filed under: Plugins, WordPress Development 2 Comments »This week WordPress 3.5 was released, and the core team has made a some great improvements to the way media is handled in the UI. I managed to get my name in the credits for a measly few lines I contributed in a couple of patches - nonetheless I’m proud to be a part of it.
The reason I created the Media Categories plugin is because of how neglected the interface for media was. Especially with regard for anyone wanting to add support for taxonomy. In 3.5 media still comes without any taxonomy enabled out of the box (as it should be) – but for those who those who do the custom work of enabling one, the interface is now much improved.
What Changed?
In the past, if you simply enabled any taxonomy for media, the editor would get a simple text field into which users would need to manually enter the slugs from memory. The interface for media was unique from that of any other post type. What they’ve done in WordPress 3.5 is simply make the Media interface behave more like every other post type.
- WordPress 3.5 Media Editor with the Category taxonomy enabled for the attachment post type.
- Media Menu with Categories enabled
- Media Library page in WordPress 3.5 is now more like the Posts and Pages interfaces.
- Taxonomy page for Media Library
So now when a taxonomy is added to media, you automatically get a built-in metabox, you get a column on the Media Library listing the taxonomy terms applied to each item, and you get a taxonomy page specifically for media. They have also greatly improved the modal window for managing media – even adding the ability to edit the details (title, caption, description) right there in the modal.
So what good is the Media Categories plugin now?
On the surface it seems as though my plugin is mostly useless now. It could be stripped down to a few lines to just enable the taxonomy and be done. Thats not the case. First of all, 3.5 still doesn’t support filtering by taxonomy in the “Gallery” shortcode. Also, since a couple of weeks before WordPress 3.5 was released, I have been working to fill in the gaps where I see some room for improvement.
Whats new in Media Categories 1.5?
The new Media modal window has at once, made it easier for me to allow users to edit the taxonomy of an image from the modal screen, while also making the default behavior problematic enough that it merited an overhaul of my plugin to address this problem.
I’ve added a collapsible version of the metabox, along with the filterable search box to the modal media editor.
- Before Media Categories 1.5 is turned on.
- After Media Categories 1.5 is turned on.
I’m not releasing it yet, because I’ve run up against a quirk in how the new modal saves data – it causes the collapsable section to immediately collapse when the auto-save kicks in. Getting the auto-save to work at all with checkboxes was a challenge.
When will Media Categories be released?
Once this auto-collapsing behavior is solved, I’ll probably be satisfied enough to release 1.5. However I do still need to test in 3.4.x and 3.3.x to make sure the plugin still works correctly in those versions. I have a vested interest in making it backward compatible because I use this plugin at work, and many of our sites are on several out dated version of WordPress (shh).
I will continue to work on it, and work with the core team to make this happen as soon as possible. However if you would like to take a look at this new version feel free to download it from the master branch on GitHub.
Update: Media Categories 1.5 Beta 2
Sears Community & Kmart Community Launched
Posted: October 1, 2012 Filed under: Plugins, Projects | Tags: community, customer service, development, kmart, launch, sears, wordpress 2 Comments »For the last few months I’ve been neglecting this blog and my publicly available plugins. Thats because 200% of my time has been tied of in this very large projects – the mySears Community and myKmart Community for Sears Holdings Corporation. This matching pair of Q&A style customer service sites have been migrated from a third-party host known as Viewpoints, and onto a self-hosted WordPress Multi-Site installation.
Today I’m happy to say that we’ve launched both.
While I’ve taken part in countless migrations to WordPress from other systems, including one from Viewpoints – this project was truely like no other – not only in the number of hours worked, but also in its technical challenges. Due to fuzzy requirements at the start of the project, the end product turned out different from what we expected on day 1 of development. If I were able to solution this project over again knowing what I know now, I would have chosen a much simpler approach. However it resulted in the development of some very interesting tools.
Of the parts I got to work on, there are 3 plugins that I’m proud of and that I think will be useful to the WordPress community – Custom Comment Type, Section Fronts, and WidgetPress. I won’t go into too much detail in this post, but heres a brief description of those three.
- Custom Comment Type – create Custom Commeny Types much the same way developers already create Custom Post Types.
- Section Fronts – A custom post type called ‘section’ that can be chosen to represent a category, post type archive, or even an intersection between the two. Similar to the way admins can already select a ‘page’ to represent the front page.
- WidgetPress - A layout management tool based on the existing Widgets API infrastructure. Overhauled to work off two custom post types and a custom taxonomy – sidebars (referred to as dropzones) can be sized and created on the fly on a page-by-page basis leveraging a css grid framework on the front end. This – especially when used in conjunction with Section Fronts – has by far the greatest potential to make an impact, and opens up a whole host of possibiities for how WordPress is used.
I’ll try to write more about each plugin in the weeks to come. For now, none of them are clean enough for me to release them publicly. I’d like the opportunity to rewrite each one from scratch and do things the right way now that I know how it all needs to work – but I dont think I’ll have that much time so I’ll have to settle for something in the middle.
I’ll also throw up some screenshots for some of these, and at some point I’ll probably create a demo video of WidgetPress in action.





