I have a need to set the wp_postmeta value (in table: wp_postmeta) on every post that has a specific term_taxonomy_id (in table: wp_term_relationships):
Specifically:
- run a query against every
post_IDthat hasterm_taxonomy_idvalue of 18 - if the
wp_postmetafor thatpost_IDdoesn't contain the key of_category_permalink_then add it with the value set to 18, otherwise ignore it
My MySQL foo is poor, so apologies if this makes no sense or I have got tables, names around the wrong way.
UPDATE: In relation to answers/comments below by @deadlyhifi and @Jot I have the following:
function cleanup_permalink() {
static $fnCount = 0; //to run only once
if ($fnCount) return;
$fnCount++;
global $wpdb;
$results = $wpdb->get_results("SELECT `object_id` FROM $wpdb->term_relationships WHERE `term_taxonomy_id` = 18");
foreach ($results as $result) {
add_post_meta( $result, '_category_permalink_', '18', true);
}
}
add_action('init','cleanup_permalink'); //i'm assuming init is the best place?

functions.phpfile. – Mike Hudson Jan 7 '12 at 10:39