How to find WordPress posts that have a featured image

One of the things I love about developing with WordPress is that you very rarely have to reinvent the wheel. Somewhere, if you can find it, is a function that does what you want.

I needed a list of post IDs that had featured images. I COULD have retrieved a complete published posts list and checked each one to see if it had a post thumbnail, but there is a much more efficient way – a new WP_Query. Set the arguments and let WP do the work for you.

Here’s the function that returns the list:

	function mgf_get_sponsor_list() {
		$args = array(	'posts_per_page'	=> -1,
						'post_type'			=> 'post',
						'meta_key'			=> '_thumbnail_id',
						'post_status'		=> 'publish',
						'fields'			=> 'ids'
		$sp = new WP_Query( $args );
		return ( ! empty( $sp->posts ) ) ? $sp->posts : false;

The $args are the key to getting what you want. I needed only posts with attached thumbnails, and I didn’t need all of the post details, just the ID. $args[‘fields’] = ‘ids’ takes care of returning only IDs, and $args[‘meta_key’] chooses only those posts with featured images.

In the above, $sp returns the whole WP_Query object, but I only needed the posts part, so the function returns only ->posts.

That’s all there is to it.

You can use the above in a theme functions file or in a plugin.

See the Class Reference/WP Query « WordPress Codex for more information on using the WP_Query class.

How To Add a Custom Body Class

If you’ve done much theme development, you’ll know how handy it is to be able to add specific classes to the <body> tag depending on which page is being viewed. Since WordPress version 2.8, that’s a simple thing to do. That version added a function called ‘body_class()‘ which, used in conjunction with the correct action, lets us easily add new classes whenever we need them, with a few lines in the theme’s functions.php file.

One of the biggest advantages to using this function/action method is that it keeps the logic out of the header.php template file, giving us cleaner coding in the template, and allowing us to split out appearance/function even more.

Using the body_class() function

Using the body_class() function is easy – write a function to add a class or classes depending on certain conditions, then ‘hook’ that function into the body_class action. (As always, a quick look at the WordPress Codex entry for body_class() can be helpful.)

One note – your theme must be coded to handle the body class function. Check the theme’s “header.php” file, look for the <body> tag. It needs to look like this:

<body <?php body_class($class); ?>>

(i.e., the tag has the call to the body_class() function). If it isn’t there, add it.

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  $classes[] = 'my-custom-class';
  return $classes;

Just adding a class like that wouldn’t do us a lot of good, though. We need some conditions, which we’ll add using WordPress conditional tags.

Adding a class to a specific page by page slug:

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  if ( is_page( 'sample' ))
    $classes[] = 'my-custom-class';
  return $classes;

Adding a class to a specific page by page ID:

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  if ( is_page( '1' ))
    $classes[] = 'my-custom-class';
  return $classes;

Adding a class to a specific category page, by category slug:

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  if ( is_category( 'custom-category' ))
    $classes[] = 'my-custom-class';
  return $classes;

Or, we can get a little more complicated…

Adding a class to specific post, but only if that post is a ‘video’ post (assuming you are using custom post types):

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  global $post;
  if ( 'video' == $post->post_type AND 'my-post' == $post->post_name )
    $classes[] = 'my-custom-class';
  return $classes;

Do you need to drill down to a very specific page/post?

## add a custom body class
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
  global $post;
  $classes[] = $post->post_name;
  return $classes;

You’re really only limited by your imagination when it comes to adding custom body classes. You can use any of the WordPress Conditional tags, you have access to the $post object for specific properties – whatever you want to do, you can probably do.


If you are at all observant, you’ll have noticed that the URL to has changed to

This is because of a very polite email from Otto at informing me that Automattic is starting to aggressively protect the WordPress trademark.

I’ve put in place automatic redirects, but I’d ask that you update any links pointing to because after the domain expires, they will not work. Thanks.

How to Display Your Post/Page Content in Two Columns

Ok, so life has intruded and I haven’t updated things here as often as I’d hoped.

So here’s a little treat: how to display your content in two columns.

Why should you display content in two columns? Narrower text columns can increase readability, especially if you have a wide ( 600px or more ) content column. There is a lot of discussion out there on the advantages of narrow text columns, give Google a shot if you’re interested.

How to do it? We’re going to use the WordPress shortcode handler, and a little bit of CSS, instead of modifying a theme template. This way, if you WANT two columns, you can have them, if not, don’t. Also, it’s still a little problematic to split a body of text automagically using scripting.

The columns usually NEVER break where you want. Using shortcodes to define the column content allows us to control what goes where.

We could create a plugin for this, but for ease of demonstration, we’ll just put it in the theme’s functions.php file. Note: with some themes like Thesis or Genesis, you may need to add this to a ‘custom’ functions file. Consult your documentation.

We will define two shortcode pairs, [leftcol] and [rightcol]. You’ll start the left column content with the [leftcol] shortcode tag, and end it with the [/leftcol] closing tag. Repeat for the right column. Shortcode tags MUST begin on a new line in your editor, or WordPress won’t recognize them as shortcodes. If you see one of the shortcode tags in your displayed post, that’s probably why.

Here is what the two columns above look like in the post editor:

[leftcol]How to do it? We're going to use the WordPress shortcode handler, and a little bit of CSS, instead of modifying a theme template. This way, if you WANT two columns, you can have them, if not, don't. Also, it's still a little problematic to split a body of text automagically using scripting.[/leftcol]
[rightcol] The columns usually NEVER break where you want. Using shortcodes to define the column content allows us to control what goes where.[/rightcol]

Here is the code:

* shortcode handler for columnization of project posts
* ex: [leftcol]content here...[/leftcol]
function shortcode_columnize_left( $atts, $content = null ) {
 $content = wptexturize( $content );
 $content = wpautop( $content );
 $content = '<div style="width: 45%; margin-right: 5%; float: left; text-align: justify; ">' . $content . '</div>';
 return $content;

/* columnize right inserts 'clear' div after content */
function shortcode_columnize_right( $atts, $content = null ) {
 $content = wptexturize( $content );
 $content = wpautop( $content );
 $content = '<div style="width: 45%; float: left; text-align: justify;">' . $content . '</div><div style="clear: both;"></div>';
 return $content;
add_shortcode( 'leftcol', 'shortcode_columnize_left' );
add_shortcode( 'rightcol', 'shortcode_columnize_right' );

We define functions to take the content that is between the shortcode tag pairs, run it through the same filters that WordPress uses for post content, wptexturize() and wpautop(), then spit it out within a div with a width of 45%, right margin of 5%, floated to the left, and the same with the right column but without the right margin. We add a div after the right content with a style of “clear: both” so that the rest of the page content clears the floated divs.

Then we tell WordPress to use our functions when it encounters the [leftcol] and [rightcol] shortcode tags within our post.

Voila, finit. That’s all there is to it.

Simple Link Cloaker Plugin Available for Download

Because some days are better than others, I managed to upload the wrong zip file of the link cloaker plugin. That error has been corrected. This is the correct download link for the Simple Link Cloaker plugin.

It seems that if I had named the Simple Link Cloaker plugin The Fabulous Redirector instead, all would have been well – but the guys/girls that run the WordPress plugin repository didn’t like the term ‘cloaker’. I guess it implies being shady, which we all know is not the intent or use for this plugin.

At any rate, since you can’t get it from WordPress any more, you can get it here: ilwp-simple-link-cloaker. Installation is a snap, if you remember the old way of installing plugins:

  1. download the plugin zip file to your computer
  2. unzip the file into a directory
  3. upload the entire plugin folder to your wp-content/plugins folder
  4. activate through the Dashboard > Plugins interface

Thank you for all of your support!

The Case Of the Missing Post

A client came to me with a strange problem. He’d written and published a post, but when he tried to view it on his site he got a 404-Not Found error.

Usually, when there is a problem with posts or pages disappearing the culprit is permalink-related. Most of the time, a simple click of the update button on the Settings > Permalink page will fix it. Not this time.

I could see no logical reason for the post to be ‘Not Found’. The permalink matched the post slug, there were no kinky characters or spaces. All of the other posts displayed as they should have. A quick switch of the theme back to Twentyten proved useless also, same problem. A look through the rewrite_rules option in the _options table proved fruitless also. Or did it?

I found the post slug within the rewrite_rules option entry; all seemed normal, until I saw this line:


Huh? This was a post, not a page.

i have an idea that..... just .... might.... work!

No, that's not me.

Creative Commons License photo credit: mil8

Then came the lightbulb moment. What if there’s a page with the same slug as the post? Not supposed to happen, but you never know.

Nothing resembling that title on the pages list.

Ahhhh, the trash can. The WordPress guys and girls had a pretty decent idea when they implemented the trash can. It takes a page or post out of circulation, but still keeps it around just in case.

Keeps it around with the original slug.

Gotcha. Sure enough, in the Pages trash can was a page with the identical slug as the post. Evidently my client had learned how to manipulate post slugs. How he was able to use a slug identical to an existing one is a question I haven’t answered yet.

Permanently delete the trashed page, and as if by magic the post now shows up where it’s supposed to.

Moral of the story: check the trash.

You Do Not Have Sufficient Permissions to Access This Page

More than a few people have contacted me after seeing this message. It usually appears after upgrading to WordPress version 3 or above, when accessing a plugin’s Dashboard menu.

This article isn’t an exhaustive treatment of this issue, but if you’re a developer you’ll at least walk away with an idea of what needs to be fixed in your plugin or theme options menus.

The cause of the “You Do Not Have Sufficient Permissions to Access This Page” error message

The common cause appears to be plugins that use an older method of generating menu and submenu items. Those are the links in your Dashboard’s menu column that appear when you activate a plugin.

When a plugin is activated, certain values are stored in the options table, one of them being a sanitized, shortened version of the plugin name. Later versions of WordPress use slightly different functions for doing this than previous versions. This is where the weirdness starts, and where some plugin developers drop the ball in their testing – previous installs of the plugin will throw errors on upgrading WordPress. Plugins installed after upgrading will be fine, with no errors.

Here is how a main menu item is generated:

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

And a sub-menu item:

add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);

There are two stumbling blocks here: the $capability and $menu_slug attributes.

First, the $menu_slug. The following is from the Adding Administration Menus page of the WordPress Codex:

The slug name to refer to this menu by (should be unique for this menu). Prior to Version 3.0 this was called the file (or handle) parameter. If the function parameter is omitted, the menu_slug should be the PHP file that handles the display of the menu page content.

Prior to Version 3.0, the value in the menu_slug attribute was used ‘as-is’, no sanitization was performed. This value populates the $_registered_pages[$hookname] value in wp-admin/includes/plugins.php. Here’s where the problem comes in: this is the value that is stored in the options table, including space characters.

Beginning in Version 3.0, space characters in $menu_slug are stripped before storage. Hence, a menu_slug with the value “My Plugin Slug” is stored as “MyPluginSlug”. Prior to 3.0, they were stored as they were passed, spaces and all.

The problem comes when the stored value is compared with the passed value in the add_menu_page and add_submenu_page calls. The new routine strips space characters from the value passed to the function, then compares against the options table entry. Surprise! They don’t match. “MyPluginSlug” <> “My Plugin Slug”.

The $capability attribute:

The capability required for this menu to be displayed to the user. User levels are deprecated and should not be used here!

This is a little easier to fix, as WordPress translates the old number-based user_level value to the new capability value. However, it’s problematic to rely on this conversion as no one knows how long it will be there.

Fixing the Blank Screen Syndrome on a WordPress Blog

With the advent of WordPress version 3, my most common fixit request has been, “Help! I just see a white screen on my blog!”

I’ll share with you what I’ve found to be the most common cause, but first there’re a few things you can do to at least get back up and running again.

If you have Dashboard access, first disable ALL of your plugins. 9 times out of 10, you’ll see your site reappear.

If you still see just a white screen, the next step is to switch themes. Activate WordPress’s default theme (the twenty-ten theme, now) and check your site again.

If that didn’t fix your problem, you’ll need to start verifying core files and such, or employ the services of a WordPress professional to help you get your site back up and running.