/**
  * Migrate all terms of SEO SearchTerm Tagging 2 on database into postmeta table
  *
  * @since 1.1
  *
  */
 public function migrate_stt2_terms()
 {
     if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'heartbeat-nonce')) {
         do_action('stt2extat_notice', $code = 9, $error = true, $add_setting_error = false);
         wp_die();
     }
     if (!session_id() && !headers_sent()) {
         session_start();
     }
     $location = 'options-general.php?page=stt2extat';
     $result = wp_cache_get('stt2extat_migrate_stt2');
     if (false == $result) {
         global $wpdb;
         $sql = "SELECT * FROM " . $wpdb->prefix . "stt2_meta ;";
         $result = $wpdb->get_results($sql);
         wp_cache_set('stt2extat_migrate_stt2', $result, 900);
     }
     if (false != $result) {
         $data = array();
         foreach ($result as $r) {
             if ('any' != stt2extat_post_type() && !in_array(get_post_type($r->post_id), stt2extat_post_type())) {
                 continue;
             }
             $ignore = get_option('stt2extat_check_relevant_terms');
             if ('1' == $ignore) {
                 $relevant = stt2extat_get_relevant_post($r->post_id, sanitize_text_field($r->meta_value), false, true);
                 if (!$relevant) {
                     continue;
                 }
             }
             $obj = new StdClass();
             $obj->post_id = $r->post_id;
             $obj->count = $r->meta_count;
             $obj->post_modified = strtotime($r->last_modified);
             $data[$r->meta_value] = $obj;
         }
         if (0 == count($data)) {
             $location = add_query_arg(array('error' => true, 'message' => 6), $location);
             wp_die($location);
         }
         global $stt2extat_data;
         $new = array();
         foreach ($data as $key => $val) {
             $stt2extat_data->last_id = $stt2extat_data->last_id + 1;
             do_action('stt2extat_nopriv_update_post_meta', $key, $val->post_id, null, $stt2extat_data->last_id, $stt2extat_data->terms, null, $val->count, $val->post_modified);
         }
         $location = add_query_arg(array('error' => false, 'message' => 15), $location);
     } else {
         do_action('stt2extat_notice', $code = 6, $error = true, $add_setting_error = false);
         wp_die();
     }
     wp_die($location);
 }
/**
 * wp query to show each post count
 *
 * @since 1.1
 *
*/
function stt2extat_count_posts($term_count, $name)
{
    $args = array('s' => sanitize_text_field($name), 'post_status' => 'publish', 'post_type' => stt2extat_post_type(), 'cache_result' => false, 'fields' => 'ids', 'posts_per_page' => -1, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
    $query = get_posts($args);
    $count = count($query);
    wp_reset_postdata();
    $term_count = (object) array('number' => $count, 'text' => _n_noop('%s topic', '%s topics'));
    return $term_count;
}