Select Page

I made an update for the post-levels plugin so it works with WordPress 2.3

The plugin was throwing sql errors.

WordPress database error: [Column ‘post_id’ in field list is ambiguous]
SELECT post_id, meta_value FROM wp_postmeta, (wp_posts LEFT JOIN wp_postmeta as pl_wp_postmeta ON (wp_posts.ID = pl_wp_postmeta.post_id)) WHERE post_id = ID AND meta_key = ‘links_to’ AND (post_status = ‘static’ OR (wp_posts.post_status = ‘publish’ OR (wp_posts.post_status = ‘private’ AND (pl_wp_postmeta.meta_key = ‘post_level’ AND pl_wp_postmeta.meta_value <= 1 ))))

WordPress database error: [Column ‘post_id’ in field list is ambiguous]
SELECT post_id, meta_value FROM wp_postmeta, (wp_posts LEFT JOIN wp_postmeta as pl_wp_postmeta ON (wp_posts.ID = pl_wp_postmeta.post_id)) WHERE post_id = ID AND meta_key = ‘links_to_target’ AND (post_status = ‘static’ OR (wp_posts.post_status = ‘publish’ OR (wp_posts.post_status = ‘private’ AND (pl_wp_postmeta.meta_key = ‘post_level’ AND pl_wp_postmeta.meta_value <= 1 ))))

Here’s the fix:

 

function postlevels_query_cleanup($sql)
{
    global $wpdb;

// fix for WordPress 2.3
// evilzenscientist – 2 Oct 07

    $sql = preg_replace(“/post_id/”, “wp_postmeta.post_id”, $sql);
    $sql = preg_replace(“/, meta_value/”, “, wp_postmeta.meta_value”, $sql);
    $sql = preg_replace(“/meta_key/”, “wp_postmeta.meta_key”, $sql);

// end

 

Here’s the new plugin. It’s not really tested – except it works for me.