Hallo zusammen,
ich beschreibe mal kurz was ich machen will: nachdem ein post/page veröffentlicht (publish) wurde, möchte ich nachschauen, ob zu dem neuen Text Tags angelegt wurden.Wenn ja, erstelle die Beziehung neu, oder ergänze der bestehenden Beziehungen.
Ich habe zwar einen Lösungsansatz, jedoch scheint er mir Durch-die-Brust-ins-Auge-Lösung zu sein:
Jedes mal wenn gespeichert wird:
überprüfe ich, ob der Beitrag den status publish hat und ob sich in der relationship Tabelle von wp etwas getan hat:
function check_delta_db( $post_id ) { global $wpdb;
global $table__terms_relation;
global $table__terms;
global $table__posts;
global $table__realtionships;
global $table__taxonomy;
//verify post is not a revision
if (!wp_is_post_revision( $post_id ) ){
update_option('stc_publish_updated_id',$post_id);
$current_post_state=$wpdb->get_var("SELECT post_status FROM $table__posts WHERE ID = $post_id"); //get current status
/*is the status of the current post = publish ? */
if ($current_post_state=='publish'){
$saved_num_of_wp_relations = get_option('stc_num_wp_relations');
$current_num_of_wp_relations=$wpdb->get_var("SELECT COUNT(*) FROM $table__realtionships ");
/*if saved number of entries is not equal than the current number of entries continue
only if a post/pages was published including tags, the $current_num_of_wp_relations is greater than the $saved_num_of_wp_relations,
because object/tags where saved in the standard term_relationship table*/
if ($saved_num_of_wp_relations != $current_num_of_wp_relations){
/*if the current post_id is already saved in the custom table update the entries (first solution: delete all items)*/
$object_is_in_table=$wpdb->get_var("SELECT COUNT(*) FROM $table__realtionships WHERE object_id = $post_id ");
if ($object_is_in_table >='1'){
if(is_admin()){
$wpdb->query($wpdb->prepare("DELETE FROM $table__terms_relation WHERE $table__terms_relation.`object_id` = $post_id"));
}
}
update_option('stc_publish_updated_id',$post_id);
$get_values ="select ".$table__terms.".term_id,".$table__terms.".name
FROM ".$table__realtionships.", ".$table__taxonomy.", ".$table__terms."
WHERE ".$table__realtionships.".object_id = ".$post_id."
AND ".$table__taxonomy.".term_taxonomy_id = ".$table__realtionships.".term_taxonomy_id
AND ".$table__terms.".term_id = ".$table__taxonomy.".term_id ";
$hpe_delta_import = $wpdb->get_results($get_values, ARRAY_A);
$current_timestamp = time();
foreach ($hpe_delta_import as $import_tag){
$import=$wpdb->insert($table__terms_relation,
array( 'object_id'=>$post_id,
'term_id'=>$import_tag['term_id'],
'term_prio'=>'3',
'timestamp'=>$current_timestamp ),
array( '%d',
'%d',
'%d',
'%d'));
}//end foreach
} // end if
}else{ // post is not in state 'publish'
add_option('stc_publish_id',$post_id);
update_option('stc_publish_id',$post_id);
/*do nothing, because no tag where added to this post or page*/
}
}// end check if post =! revision
}
Alles anzeigen
Leider ist die aktuelle Lösung so, dass ich bei einem update alle Beziehungen entferne und dann neu aufbaue. Das ist nicht schön. Wie kann ich denn geschickt update und insert Befehle einsetzten? Jeden Wert in einer Schleife vorher abklopfen scheint mir doch etwas zu übertrieben, vor allem da es viele Tags zu einer Seite geben kann.
Vielen Dank mal wieder...