Category Archives: Tutorials

WordPress $wpdb basics

Access WordPress Database

Starship $wpdb

Recently I’ve been adapting a plug-in to suit my needs and for the first time, trying to write something that interacts with the wordpress database, step up wpdb class!

The WPDB Class – Make it Global

To use the wpdb class we need to access a global variable $wpdb which is simple enough, but what you’ve got to remember is that it needs to be globalized before you use it in your custom function: –

global $wpdb;

Once that’s in place you are free to use it to perform SQL queries and function on your database.

The first thing I needed to do was to query the “commentmeta” table in the database, and save the results for later use: –

$comment_meta = $wpdb->get_results( "SELECT mydata FROM mytable WHERE mycondition = 'myvalue' ");

The above code uses ‘get_results’ to query the database and loads in to the the variable $comment_meta so it can be used in a ‘foreach’ loop later. Now I wanted to grab the meta_key, meta_value from table ‘commentmeta’ that was named ‘wp_1_commentmeta’. I could have put that value into the query but if I want to use the function on a different site on a wordpress network I’m not going to know the database prefix so I can use $wpdb to replace it: –

$comment_meta = $wpdb->get_results( "SELECT meta_key, meta_value FROM wpdb->commentmeta ");

That way I can use the query in a function on any wordpress blog and it will always knows what table to look at.

It’s an Object by default

One last thing to remember is that the results are an object so you can access the like this: –

foreach ( $comment_meta as $comment_meta_data )
echo $comment_meta_data->meta_key
echo '< br />'
echo $comment_meta_data->meta_value

And that’s what I wanted but if you want something different like an array then you can use OBJECT_K for an associative array of row objects, using first column’s values as keys, ARRAY_A for a numerically indexed array of associative arrays, using column names as keys or ARRAY_N for a numerically indexed array of numerically indexed arrays like this: –

$comment_meta = $wpdb->get_results( "SELECT meta_key, meta_value FROM wpdb->commentmeta", ARRAY_N );

for more information on the wpdb class check out the wordpress codex

DNS wildcards on cpanel for wordpress mu

How to add a wildcard entry so your wordpress mu subdomains resolve correctly

If you want to use sub domains for your wordpress mu blogs you are going to need to add a DNS wild card to get it to work. Thankfully if your host provides cpanle there’s a really easy way to do this. Here’s a screencast showing you how.

[screentoaster stUk9RREZIR11bSF5aXl5YVFZT]

Shopify : The current tag as a title


Using tags in Shopify to create nested navigation is a great  way to help customers find products but once they’ve browsed to the tag page you need to let your customers  know what they’re looking at. A collection has the following property for this : –

{{ product.title }}

That works well for collections but tags are slightly different firstly a tag doesn’t have a title so you can’t have{{ tag.title }}. Tags are attributes of products and collections but if you use {{ collection.tags }} you are going to get a list of all the tags associated with the collection, which is not what we want. The function to use is : –

{{ current_tags }}

And voila this will output the current tag which you can use as a title by wrapping it in some <h2> tags or what ever you want. Nice and simple when you know how.