brandontreb.com Tips And Resources For Software Consultants

WordPress Coding: Programmatically Add Post Tags (and other meta info)


I was recently working on a script that imports from a custom blogging platform into WordPress and had some need to programmatically add post keywords.

The script to do this is actually quite simple and can be used to update any post attribute.

Here is the code:

// Create the post array
$post = array(
    ‘ID’ => 5,
    ‘tags_input’ => ‘foo,bar,baz’);     
 
// Update the post
wp_update_post($post);

This will assign the keywords “foo”, “bar”, and “baz” to the post with ID 5. This task seems trivial, however it’s very powerful when you think about automation. For example, you could write a script to scrape a google search for your target keyword and find related keywords for each of your posts automatically. Hrm… plugin idea?

Give it a shot. More info on available parameters can be found on WordPress’ site here

Here are some of the other fields that you are able to update this way:

defaults = array(
‘post_status’ => ‘draft’, 
‘post_type’ => ‘post’,
‘post_author’ => $user_ID,
‘ping_status’ => get_option(‘default_ping_status’), 
‘post_parent’ => ,
‘menu_order’ => ,
‘to_ping’ =>,
‘pinged’ =>,
‘post_password’ =>,
‘guid’ =>,
‘post_content_filtered’ =>,
‘post_excerpt’ =>,
‘import_id’ => );

Happy WPCoding!

WordPress Programming Tip: Enable Database Error Reporting For Custom Queries

So this one should seem pretty obvious, but it wasn’t apparent to me at first.  It was only after digging through the wp-db.php file that I discovered how to enable error reporting.

The Problem

As you may have discovered, the wp_query() function isn’t a “one size fit’s all” solution.  Often times, you may need to query the WordPress database using a custom MySQL query.  Especially  if you are using WordPress for anything other than a blog (ie freshapps.com).

When writing custom queries, it can often be frustrating if you make a mistake in the SQL syntax as WordPress will simply display no results.  For example:

$results = $wpdb->get_results("SELECT * FROM $wpdb->posts 
   WHERE post_title = ‘foo bar baz");
print_r($results);
 
// Outputs Array ( )

Since we have made an error in our SQL statement (I didn’t add the second single quote), WordPress will suppress it and simply return an empty array. This is not very helpful for debugging.

The Solution

The solution is actually quite simple. The global $wpdb object has a property called show_errors. Setting this property to true will cause WordPress to output the SQL errors to the screen for a given query.

Here it is with our example above

// Enables Wordpress’s DB Error reporting
$wpdb->show_errors = true;
 
$results = $wpdb->get_results("SELECT * FROM $wpdb->posts 
   WHERE post_title = ‘foo bar baz");
print_r($results);
 
// Outputs 
// WordPress database error: [You have an error in your 
// SQL syntax; check the manual that corresponds to your 
// MySQL server version for the right syntax to use near 
// ”foo bar baz’ at line 1]
// SELECT * FROM wp_posts WHERE post_title = ‘foo bar baz

Now we know what went wrong with our query rather than just receiving empty results.

Let me know if you have any questions or comments.

Happy WPCoding!

Reblog this post [with Zemanta]

Emacs For OSX Is Out!

For all you Vi using, Emacs haters out there, I will fight you!

The iPad Is Out And It Sounds Like iPod While Plugging Your Nose

Watch Out Apple, The Kindle Dev Kit Is Almost Live

With the success of the Apple App Store, it seems that everyone is wanting a piece of the pie.

Amazon is now looking to throw their hat into the ring and is releasing their own dev kit for their ever so popular Kindle.

http://www.amazon.com/gp/feature.html/?ie=UTF8&docId=1000476231

It appears that big name game developers Electronic Arts is also getting involved (WTH?!?!)

This just seems like it will be an epic fail. Who really wants to play video games on their Kindle? Maybe they can port Kirby’s Dreamland from the original Game Boy :) .

Reblog this post [with Zemanta]