When using WordPress post formats, you’ll quickly start looking for a way to query WordPress content by the “standard” post format. This Trac ticket seeks to work on solving this. The implementation there handles URL-based requests:
http://example.com/type/standard/
quite nicely. However the code in that patch doesn’t (yet) handle direct WP_Query calls.
I was hacking on this a bit on my own before finding the (much more elegant and complete) code that Nacin contributed. I came up with some working code that does work with WP_Query calls, which I posted in this Gist:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function akv3_query_format_standard($query) { | |
| if (isset($query->query_vars['post_format']) && | |
| $query->query_vars['post_format'] == 'post-format-standard') { | |
| if (($post_formats = get_theme_support('post-formats')) && | |
| is_array($post_formats[0]) && count($post_formats[0])) { | |
| $terms = array(); | |
| foreach ($post_formats[0] as $format) { | |
| $terms[] = 'post-format-'.$format; | |
| } | |
| $query->is_tax = null; | |
| unset($query->query_vars['post_format']); | |
| unset($query->query_vars['taxonomy']); | |
| unset($query->query_vars['term']); | |
| unset($query->query['post_format']); | |
| $query->set('tax_query', array( | |
| 'relation' => 'AND', | |
| array( | |
| 'taxonomy' => 'post_format', | |
| 'terms' => $terms, | |
| 'field' => 'slug', | |
| 'operator' => 'NOT IN' | |
| ) | |
| )); | |
| } | |
| } | |
| } | |
| add_action('pre_get_posts', 'akv3_query_format_standard'); |
Note that this doesn’t create the taxonomy term and do some of the other nice set-up that Nacin’s code does. It could use a bit of cleanup – improvements are very welcome. This isn’t going to be at the top of my list for a while, so I wanted to make sure I published the code so anyone else who is interested can take a stab at merging the two approaches.
For my own usage, I wanted to have the list of recent “standard” posts that you see in my sidebar. To create this, I enabled the code in the Gist above, then made a slight mod to the Recent Posts widget that is included in core. If you’ve wanted the same, please grab the code and enjoy.
Note for future visitors: this code is working as desired with WordPress 3.3.x, but will hopefully not be needed in some future version.
This post is part of the project: Post Formats Admin UI. View the project timeline for more context on this post.
Alex King: WP_Query by “Standard” Post Format: When using WordPress post formats, you’ll quickly start looking f… http://t.co/VNZ5YLKa
New on @alexkingorg: WP_Query by “Standard” Post Format http://t.co/bLQJTYQh
This is super-handy. Thanks for posting.
[…] You are here: Home > Links > How to query by “standard” post format PermalinkHow to query by “standard” post formatBy Mark McWilliams 1 min agoFebruary 6, 20120 var addthis_product='wpp-263';var […]
[…] not having much luck? It turns out there’s a Trac Ticket for this very subject. But for now, Alex King came up with a way for it to work with WP_Query calls. Go have a look at his […]
@alexkingorg I just saw http://t.co/MpgA6e7p. I’ll take a stab. 🙂
@nacin excellent, I look forward to seeing your take on it.
WP_Query by “Standard” Post Format http://t.co/ikB02kxn via @alexkingorg WP Ticket http://t.co/5PjwxUPZ
WP_Query by “Standard” Post Format – http://t.co/VaF7w95g
Thanks a lot that was really helpful.