Title: Bug with APT trigger and Replace?
Last modified: August 30, 2016

---

# Bug with APT trigger and Replace?

 *  Resolved [Markus Odevall](https://wordpress.org/support/users/vampyrse/)
 * (@vampyrse)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/)
 * Hey there,
 * I have my APT (1.8.1) set to running only on “Imported”, and “Replace old terms
   with newly generated ones” _without_ “Remove old terms if new ones aren’t found”.
 * It’s not following those settings, not even after I reinstalled. 🙁
 * First of all, it removes old terms even when it doesn’t find new ones. All posts
   are set to the default term when imported, but by the time APT has run a lot 
   of them are left with no category – as the default is removed and no new term
   added.
 * Second of all, I should be able to add my own terms with the above settings, 
   right? I can only do it in regular edit – when I do it with Quick edit, the new
   terms are removed on save.
 * [https://wordpress.org/plugins/automatic-post-tagger/](https://wordpress.org/plugins/automatic-post-tagger/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Devtard](https://wordpress.org/support/users/devtard/)
 * (@devtard)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568137)
 * Hi. Could you please export all data from APT and send it together with some 
   sample post to my Gmail address (I use the same handle)? I’ll try to recreate
   the issue.
 * Yes, I suppose you should be able to edit the terms – that makes sense when APT
   is set to run only when importing posts. I haven’t looked into this, but it’s
   possible that the same hook used by APT for handling imported posts (“wp_insert_post”)
   is also used by WP when updating posts using the “Quick edit” feature. You should
   still be allowed to edit terms when using the post editor. Right?
 * I’ll add the hook-related issue on my to-do list.
 *  Thread Starter [Markus Odevall](https://wordpress.org/support/users/vampyrse/)
 * (@vampyrse)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568281)
 * I’ve sent you the info you requested.
 * Yeah, seems like it most be using that hook – as odd as it seems. It’s probably
   adding the term, but then removing it right after when APT don’t find a match.
   Hopefull, taking care of the latter bug will fix the former.
 *  Plugin Author [Devtard](https://wordpress.org/support/users/devtard/)
 * (@devtard)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568331)
 * Thanks.
 * Unfortunately, I was unable to recreate the bug you reported, and also failed
   to find a hook that would be triggered only when importing new posts. But here’s
   what you can do regarding the second issue:
 * Find the function “apt_single_post_tagging” in the main PHP file and put there
   the following code (e.g. somewhere around the line 2861):
 *     ```
       global $pagenow;
   
       if($pagenow == 'admin-ajax.php'){
       	return;
       }
       ```
   
 * This should stop the tagging function from being run when the Quick Edit feature
   is used.
 *  Thread Starter [Markus Odevall](https://wordpress.org/support/users/vampyrse/)
 * (@vampyrse)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568339)
 * Thanks for the support!
 * I’ve digged deeper to find the problem, and have found the bug – or misunderstanding?
   Removed the entire installation, and cleared the database.
 * Installed again and added a new test category in the keyword set. Ran it, and
   it worked smoothly – all posts that it could categorize kepts its original categories.
   Plus, I could add and remove categories using the quick edit.
 * I then imported the old keyword sets. Lo and behold, again the same problems 
   as before occured. Both of them. Removed them one more time, added a test category
   and again it worked great.
 * So there seem to be something with the keywords. Could it be the queries for 
   multiple term types that mess stuff up?
 * I tested adding one category and one custom term, and as soon as I did that, 
   the errors occured again – but now it wasn’t in general, only for some posts.
   Analyzing these posts I saw a pattern:
 * When you add multiple different term types and check the setting “Replace old
   terms with newly generated ones”, the algorithm removes all old terms, even the
   ones that are of a different type.
 * In my case: I have added two term types “Category” and custom “Geography”. With
   the above settings, when a post finds a match for “Geography” it also removes
   the category of the post.
 *  Thread Starter [Markus Odevall](https://wordpress.org/support/users/vampyrse/)
 * (@vampyrse)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568340)
 * So, I think I solved the problem.
 * Changed in the main file, adding an if (count()) to make sure that when the replacement
   is made, the corresponding term must have a match. Like so:
 *     ```
       if($apt_number_of_added_terms_total > 0){ //if the plugin found some keywords, replace the old terms - otherwise do not continue!
       				if (count($apt_single_taxonomy_with_found_terms_array[1]) > 0) wp_set_object_terms($apt_post_id, $apt_single_taxonomy_with_found_terms_array[1], $apt_single_taxonomy_with_found_terms_array[0], false); //replace terms
       			}
       ```
   
 *  Plugin Author [Devtard](https://wordpress.org/support/users/devtard/)
 * (@devtard)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568344)
 * Wow, awesome! 🙂 Sorry for not being able to reply sooner. I just realized that
   I’ve actually dealt with this issue already, but completely forgot about it –
   my apologies.
 * This is what the loop looks like in the development version:
 *     ```
       $apt_number_of_added_terms_all_taxonomies = 0;
   
       ### ADDING TERMS TO THE POST according to their taxonomies
       foreach($apt_taxonomies_with_found_terms_array as $apt_single_taxonomy_with_found_terms_array){
       	$apt_number_of_added_terms_single_taxonomy = count($apt_single_taxonomy_with_found_terms_array[1]); //for debugging
   
       	if($apt_number_of_added_terms_single_taxonomy > 0 and ($apt_settings['apt_old_terms_handling'] == 1 or $apt_settings['apt_old_terms_handling'] == 3)){ //if terms were found by the plugin; if the post has no terms, we should add them - if it already has some, it won't pass one of the first conditions in the function if $apt_settings['apt_old_terms_handling'] == 3
       		wp_set_object_terms($apt_post_id, $apt_single_taxonomy_with_found_terms_array[1], $apt_single_taxonomy_with_found_terms_array[0], true); //append terms
       	}
       	if($apt_settings['apt_old_terms_handling'] == 2){
       		if($apt_number_of_added_terms_single_taxonomy > 0){ //if the plugin found some keywords, replace the old terms - otherwise do not continue!
       			wp_set_object_terms($apt_post_id, $apt_single_taxonomy_with_found_terms_array[1], $apt_single_taxonomy_with_found_terms_array[0], false); //replace terms
       		}
       		else{ //no new terms/keywords were found
       			$apt_current_taxonomy_terms = wp_get_post_terms($apt_post_id, $apt_single_taxonomy_with_found_terms_array[0], array('fields' => 'names'));
       			$apt_current_taxonomy_term_count = count($apt_current_taxonomy_terms);
   
       			if(($apt_settings['apt_old_terms_handling_2_remove_old_terms'] == 1) and ($apt_current_taxonomy_term_count > 0)){ //if no new terms were found and there are old terms, remove them all
       				wp_delete_object_term_relationships($apt_post_id, $apt_single_taxonomy_with_found_terms_array[0]); //remove all terms
       			}
       		} //-else
       	} //-if the user wants to replace old terms
   
       	$apt_number_of_added_terms_all_taxonomies += $apt_number_of_added_terms_single_taxonomy;
       } //-foreach
       ```
   
 * Originally I planned to release v1.9 ASAP, but unfortunately haven’t been able
   to get to it yet. There are still many things that need to be modified and implemented.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Bug with APT trigger and Replace?’ is closed to new replies.

 * ![](https://ps.w.org/automatic-post-tagger/assets/icon-256x256.png?rev=1049934)
 * [Automatic Post Tagger](https://wordpress.org/plugins/automatic-post-tagger/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/automatic-post-tagger/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/automatic-post-tagger/)
 * [Active Topics](https://wordpress.org/support/plugin/automatic-post-tagger/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/automatic-post-tagger/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/automatic-post-tagger/reviews/)

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [terms](https://wordpress.org/support/topic-tag/terms/)

 * 6 replies
 * 2 participants
 * Last reply from: [Devtard](https://wordpress.org/support/users/devtard/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/bug-with-apt-trigger-and-replace/#post-6568344)
 * Status: resolved