hi. i need help. i want to delete posts where its title != *favourite* e.g. if title does not contain the word favourite, then it will de deleted. function deleteNoMatchPosts(){ global $wpdb; $wpdb->query(" DELETE FROM $wpdb->posts WHERE post_title!='*favourite*' "); } add_action('publish_post', 'deleteNoMatchPosts'); is it correct?
!= is not used in mysql. DELETE FROM $wpdb->posts WHERE post_title NOT LIKE '%favourite%' " % is used in sql as a wildcard char, so it should be used if you meant * as a wildcard. if you meant the actual * char, it should be like this: DELETE FROM $wpdb->posts WHERE post_title NOT LIKE '*favourite*' "
Not strictly true. In this instance it wouldn't work since a LIKE is being used, however it is valid in mysql to use != in the format: SELECT field1 FROM table WHERE field1 != 'value'
THANK YOU very much. now i want to extend it.. DELETE * FROM wp_posts WHERE post_date > '2009-05-01 00:00:00' AND post_title NOT LIKE %favourite%' doesnt work. please somebody fix it
DELETE * FROM wp_posts WHERE post_date > '2009-05-01 00:00:00' AND FROM wp_posts WHERE post_title NOT LIKE %favourite%' doesnt work too
Looks like you are missing a single quote at the start of %favourite%: DELETE * FROM wp_posts WHERE post_date > '2009-05-01 00:00:00' AND post_title NOT LIKE '%favourite%'