Things to Know for Every WordPress Developer

WordPress is certainly a great tool, and it’s quite easy to get started. You can count on a lot of good resources (like this humble blog) and you can even dig into WP’s source code if you know PHP quite well.

But also, it’s quite easy to get lost. There are a lot of options, a lot of solutions, and sometimes it’s way harder to find the best solution than just fixing an issue. Well, let’s see some basic tips and snippets to help you solve some everyday issues.

You really shouldn’t use query_posts()

The truth is, among many reasons, you shouldn’t ever use query_posts. This is the most simplistic version of the loop, but as jQuery code, it’ll run a lot of background operations that will mostly rely on the WP_Query (then get_posts) and will require you a lot of code to clean up the mess.

In short, WordPress will load the main query BEFORE calling template files, so if you call a query_post() in your index.php file you’re actually calling 2 queries since the first one was already called. And if you consider the background queries it’s actually 8 (since each WP_Query loads 4 queries, call posts, count posts, call metadata, call terms).

What you should do:

Use WP_Query object whenever you need multiple loops in a page. So, sidebar loops, secondary loops in a page template and anything like this will be better using this function
Use pre_get_posts filter to modify the main loop. If you need to modify in any way the main WordPress loop (pretty much the case that you would use query_posts) just go for the pre_get_posts filter since it modifies directly the main WP_Query object (instead of getting a new DB query)
Use get_posts() if you don’t need a loop. If you are just getting posts and don’t need the main loop functions you could use this one since it’ll return you a simple array of posts

Always enqueue your scripts & styles

When you are creating themes, plugins or customizing any of these you may need to load external files. But each WordPress can contain a lot of things, and if you call a JS library twice you can potentially break the site.

The simple solution is introduced with the wp_enqueue_script function, since you can load (and register) a library or script and make sure that you are loading only one copy of it. The same rule applies for styles, but they won’t cause too much damage. Still a good option for loading standard styles, like styling for jQuery plugins, or CSS for HTML bootstraps.

Cache your stuff

If you are a plugin developer you should know the transients API. They allow you to store “options” for a small amount of time. So, if you are getting latest tweets, for instance, there’s no point in loading them all the time, you can set a transient for it and only load every 15 minutes or so.

The great thing about it is that you can cache your entire query on it. So if your blog is updated once a day you can set a transient that expires every 12h or less and you’ll be fine.

Know all your feeds

It’s always good to provide your usual feeds to your users, but sometimes we need a little bit more. Actually there are quite a lot of cool feeds that you can use:

Main –
Main comments –
Post comments –
Categories & tags – or
You can also include / exclude categories like this –,25,17&feed=rss2 or this
Author –
Search –
Custom Post Type –
Custom Taxonomy –

How to add featured image in your feed

This one is quite simple but gives a good final result. This code will do it (in your functions file):

function featured_image_in_feed( $content ) {

global $post;

if( is_feed() ) {

if ( has_post_thumbnail( $post->ID ) ){

$output = get_the_post_thumbnail( $post->ID, ‘medium’, array( ‘style’ => ‘float:right; margin:0 0 10px 10px;’ ) );

$content = $output . $content;



return $content;


add_filter( ‘the_content’, ‘featured_image_in_feed’ );

Optimize your DB once a while

You can use either a plugin or manually but it’s always good to optimize your MySql tables often (at least once or twice a month) so you’ll ensure that your queries are as good as they can be, and will reduce your DB size

Enable GZIP

Imagine how great would it be if you could compress your site files before sending them to the user? Well, with server-side GZIP you can do that. And it’s fairly simple, you can add this snippet to your .htaccess file and you’re good to go:


<ifmodule mod_deflate.c>

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript


#End Gzip

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

There’s a plugin for that

Even if you are not a developer you could improve your site performance with caching plugins, DB optimization plugins, CSS and JS minifying plugins.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.