Get Only Posts With Featured Images

A neat trick you might need sometimes is to enable users to only show posts with featured images (galleries, etc). This is easily done using the WP_Query class like so:

$args = array(
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'order'            => 'DESC',
    'meta_query'       => array(
            'key'     => '_thumbnail_id',
            'value'   => '',
            'compare' => '!=',
$awesome_posts = new WP_Query( $args );

You can add multiple meta fields to query with, just take a look at the Codex

The Missing WordPress Body Hook

WordPress is awesome, but one of the top features not yet included on my list is the availability of a built in “just after the opening body tag” hook. You can hook anything into the header using wp_head() or anything just before the closing body tag using wp_footer() but there’s nothing for the opening body tag, so let’s code it ourselves.

Why? Most prominently, Facebook recommends – when using the Javascript SDK – to put some javascript just after the initial body tag. If you’re building a theme for sale you’ll want to do this with hooks to make it easier for users to add their own API details.

To remedy this, we can use a custom hook, it’s really very easy. First, create a function that will act as our hook.

/* Header Stuff */
<?php my_body_hook() ?>
/* Body Stuff */

Next, let’s create this function and make it run all the functions associated with a specific hook.

function my_body_hook() {
    do_action( 'my_body_hook' );

Now all we need to do is hook other functions into this.

add_action( 'my_body_hook', 'add_facebook_sdk' );
function add_facebook_sdk() {
    echo '<div class="fb-root"></div>'

Note that the ‘my_body_hook’ name does not need to match the action name in the do_action() function, but it is easier to leave it the same.

Meta Boxes For Page Templates

Adding meta boxes to posts is fairly easy using the documentation found in the Codex. Restricting them to post types is a no-brainer, the functionality is built in using the fourth parameter of the add_meta_box() function. How about restricting meta boxes to page templates? All you need is 2 extra lines of code to detect which post you are currently on when using the function.

add_action( 'add_meta_boxes', 'my_meta_boxes' );

function my_meta_boxes() {
	$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

	if( get_post_meta( $post_id, '_wp_page_template', true) == 'template-mashup.php' ) {

	        __( 'Select Content Types', TEXTDOMAIN ),


All I’ve added is the detection of the post ID and to determine the template I grab the info from the _wp_page_template meta field.

Automated Beanstalk Revision Notifications

I use Beanstalk as an SVN server and I absolutely love it! One of the great features it has is Webhooks which can run before and/or after a deployment. You can read a bit about this on the Beanstalk Support Center, but here’s a quick and dirty working example.


$data = @file_get_contents('php://input');
$data = json_decode( $data );

	create_function('', 'return "text/html";')

$headers[] = 'From: Daniel Pataki <>';

		"<?php echo $data->comment ?>"

	<h3 style='margin:0 0 3px 0; padding: 0 0 3px 0; border-bottom:1px solid #ddd'>

			<td style='text-align:right; padding-right:11px;'>
			<td> <?php echo $data->repository ?></td>
			<td style='text-align:right; padding-right:11px;'>
			<td> <?php echo $data->revision ?></td>
			<td style='text-align:right; padding-right:11px;'>
			<td> <?php echo $data->deployed_at ?></td>
			<td style='text-align:right; padding-right:11px;'>
			<td> <?php echo $data->environment ?></td>
			<td style='text-align:right; padding-right:11px;'>
			<td> <?php echo $data->author_name ?></td>

$message = ob_get_clean();

	$data->repository . ' | new revision (' . $data->revision . ')', 

I include wp-load.php just because WordPress is there anyway and it gives me access to wp_mail(). You can use the regular old mail() function or any other method of course.

The point here is that the data can be retrieved using file_get_contents('php://input') and once you json_decode() it you can use all the available parameters. If you use my code, you’ll get something like the image below in Gmail.

An email triggered by a Beanstalk Webook

Get All Google Web Fonts

Google Web Fonts is a wonderfully easy way to add beautiful fonts to your website using CSS only, if you’re still using cufon, shame on you!

Using Google Web Fonts is very easy, but if you build themes or similar things you might want to show your users a list of available fonts. It turns out this is pretty easy to do, just get the contents of the following url.

To get an API key you might want to visit the Developer Console. From here on out it’s a trivial matter to present the data, especially since the data is returned in json.

$fonts = json_decode( 
        file_get_contents( '' . WEBFONTS_API_KEY ) 

Avoiding Counters In Loops

When writing code I try to keep things clean but one thing has always bothered me, using $i (or something similar) as a counter, incrementing it after each step. Here’s an alternative approach which you can use with arrays to get around those pesky counters.

$navigation = array( 'Home', 'Contact', 'About' );

foreach( $item = current( $navigation ) ) {
    $current = ( key( $navigation ) == 0 ) ? 'current' : '';
    echo '<a class="' . $current . '" href="#">' . $item . '</a>';
    next( $item );

While not much shorter, it is much better because the current value is now inherently contained inside the actual array keys. If something doesn’t work here you can be pretty sure the problem is in the loop and if it seems to work, you can be pretty sure it actually does.

If you haven’t seen this before take a look at the current() function on the PHP codex, it has all the similar functions listed below for moving pointers and such, very handy!

WordPress Pagination Links

For ages and I ages I used a plugin for WordPress pagination when there has been a solution since 2.1 to do this easily! Don’t get me wrong, wp_pagenavi is a great plugin but I prefer not to use plugins needlessly.

The paginate_links() function allows you to create easy-to-customize pagination which you can use outside the loop as well.

$list = new WP_Query($query_args);
$pagination = array(
	'base'       => str_replace( 99999, '%#%', get_pagenum_link( 99999 ) ),
	'format'     => '?paged=%#%',
	'current'    => max( 1, get_query_var( 'paged' ) ),
	'total'      => $list->max_num_pages,
	'next_text'  => 'next',
	'prev_text'  => 'previous'			
echo '<div class="pagination primary-links">';
echo paginate_links( $pagination );
echo '</div>';

Since the function does not use the loop directly you can use it for listing anything as long as you know the current page and the total number of pages.

$list = range(1, 200);
$items_per_page = 12;
$pagination = array(
	'base'       => get_bloginfo( 'url' ) . '/mypage/%_%',
	'format'     => '?paged=%#%',
	'current'    => $_GET['current_page'],
	'total'      => ceil( max($list) / $items_per_page ),
	'next_text'  => 'go forth',
	'prev_text'  => 'go back'			
echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>';