Can someone tell me how to modify the code below so that it filters out revisions? I tried using revision deletion plugins but they only work for revisions of posts or pages… not custom post types.
<?php
global $wpdb;
global $post;
$review_id = $post->ID;
if (get_post_type($review_id) == "features") {
$assoc_id = get_post_meta($review_id, "associated_hosts_value", true);
$review_id = intval($assoc_id);
}
$assoc = $wpdb->get_col("SELECT DISTINCT post_id from wp_postmeta WHERE (meta_key='associated_hosts_value') AND ((meta_value LIKE ',{$review_id},%') OR (meta_value LIKE '%,{$review_id},%') OR (meta_value = '{$review_id}') OR (meta_value LIKE '%,{$review_id}'));");
Revision is stored in
wp_postsunder the post_type column, so you will need to inner join on wp_posts and query for apost_type NOT IN ('revision'):By the way, inserting raw SQL queries in to get_col is a bad practice, as it leaves you vulnerable to SQL injections. Use
$wpdb->prepare()as shown here:http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
Note that you will need to escape the
%used in the LIKE statements by changing them to%%to work withinprepare().Also, you shouldn’t hard code WP table names in to your query statements. The wpdb class stores table names for use in constructing your queries, i.e.
$wpdb->posts,$wpdb->postmeta. Using these assures that your plugin will always query the correct WP tables. Try your code in a multi site installation to find out why this is important.