WordPress Taxonomy Archive Parent Items Filter

If you’ve ever delved into the WP core you’ll find some little items that are quite unknown. Take tax_query for example; most developers are aware of the standard options: ‘taxonomy’ ‘field’ and ‘terms’ but did you know there is an ‘include_children’ option? This returns only direct parent matches for the taxonomy terms specified – in other words, no child terms if you are using hierarchies. This is pretty straightforward for custom queries, but what about archive pages? Here’s a filter for that:

// Custom Taxonomy Page Filter
add_action('pre_get_posts', 'my_taxonomy_children_filter' );
function my_taxonomy_children_filter( $query ) {

global $wp_query;

if ($query->is_tax) {

$modded_tax_query = $query->tax_query;

$filter_tax = $modded_tax_query->queries[0]['taxonomy'];
$filter_terms = $modded_tax_query->queries[0]['terms'];
$filter_field = $modded_tax_query->queries[0]['field'];

$query->set('tax_query', array(array(
'taxonomy' => $filter_tax,
'field' => $filter_field,
'terms' => $filter_terms,
'include_children' => false
))
);

$query->parse_query();
}

return $query;

}

Works pretty well for me. Pop that into your functions.php and it should do the trick :-) Let me know if it doesn’t for you!

0 Comments , , ,

Debugging WordPress

I’ve been working on a performance issue with a for loop in a theme in WordPress for the past week, in particular trying to get the number of queries down. The homepage was executing around 10000 queries to the DB, which while it executed each query quickly due to the SQL being optimized, was ultimately slowing the site down. So after several pointers from various people, I spend some time with the code and managed to get it down to 290 queries after optimizing the loop structure. To do this I used 2 plugins that were a MASSIVE help:
Debug Bar and Debug bar Console
If you are a WordPress developer, I highly recommend using these as they really make the debugging process far easier due to their comprehensive info panels that you can switch on and off.

Sidenote: If you are using foreach to loop through an array and you only want a cross section of the results, try using the array_slice function instead of using a counter variable.

5 Comments , ,

Some thoughts on DB Queries

Lately there have been a couple of posts in the WordPress community about DB queries and the amount of calls that are made per page. While that clearly has merit and I’m by no means saying ignore it, there’s been very little focus on the actual DB queries themselves. No offense to anyone, but if you are going to focus on the number of DB calls instead of first optimizing your SQL – then either you know very little about development, or have never built a large scale web application.

So, if you have queries that have any kind of JOIN in them, I would suggest restructuring those as a first port of call. ONLY once you’ve done that should you move onto optimizing the code. I hit a performance related issue this past week and while it ended up having to do with the amount of DB calls, the problem wasn’t the SQL queries in that case but as my general rule of thumb I checked them first. That allowed me to figure out that the problem was in the code.

What do you think about optimization?

0 Comments , , ,

Cart66 WordPress Plugin Discount

Got an email from AppSumo today for a special on the Cart66 e-commerce and membership plugin for WordPress. Only $45 for the next 14 hours! That’s basically half price. If you want it, now is the time to buy! Click the title link above to get the special!

0 Comments , , , ,

GPL Driven Development

Over the past few days I’ve been thinking about how it is that a .NET and C++ trained developer like myself came to be in the world of open source and WordPress of all things. And along with that I’ve been encouraging/educating some people to learn how to code in PHP and WordPress. So they usually ask me for books and/or tutorials, but I never really had that when I started with PHP. I simply dived right into the code and, with much praying, hoped I didn’t screw up!

Typically in the WordPress and PHP community a lot of guys tend to moan and groan about the GPL and how it’s screwing things up for the commercial angles, well I’m not going to comment on that as I think most of those people are just making it worse, but what I will say is that it’s helped me a tremendous amount in terms of learning PHP and WordPress.

When I started developing in PHP almost 5 years ago I had no-one locally that I could ask for help as all my mates were .NET developers, and coming from that closed source environment (at that time) I was very hesitant to learn another language. But what I found surprised me – I found a community willing to share and help me. The GPL and open source, in general terms and in the spirit it tries to encourage, allowed me to dig deep into already existing code and learn from others who went before me and wrote some pretty kickass code! And the best part is now that I work for a company who uses the GPL – WooThemes – our users can improve their skills by simply looking at code that I’ve helped write.

And that’s pretty cool in my opinion and why I’m likely not to go back to closed source platforms :-) What do you think and how did you learn to code?

Disclaimer: This is my personal view and does not necessarily reflect the views of WooThemes and/or any of their employees.

1 Comments , , , ,

A New Coat of Paint

I’ve been saying I’ve been wanting to put up a new design for my blog for quite a while now, but as it happened I just never got around to it :-( so last night I decided enough was enough, so here it is – the new Jeffikus.com :-) It’s based on Elefolio, which makes sense seeing as I work at WooThemes! And I figured it was about time that the Tumblog plugin author starts actually using the Tumblog plugin!!! Kinda cool to use your own code for yourself. I plan on making a couple of design improvements, starting with my logo. Any thoughts or suggestions?

0 Comments , , ,

WooTumblog, Post Formats, Express App – WordCamp Spain 2010 Slides

As promised, here are my slides from WordCamp Spain 2010!

2 Comments , , , , ,

Google Translate

Google Translate really is amazing. I’m sitting here at WordCamp Spain and I’m using it to understand as much as I can :-) So far I’m following ok.  Quite a fun conference!

0 Comments , , , ,
Page 2 of 512345