Skip Navigation

Resolved

Reported for: WPML Multilingual CMS 4.5.8

Resolved in: 4.5.9

Overview of the issue

If you are using a different prefix than wp_ for your WordPress database, you may see an error indicating that the wp_terms table does not exist.

Table ‘wp.wp_terms’ doesn’t exist for query…

Workaround

Make a full backup of your site before proceeding.

  1. Open the /plugins/sitepress-multilingual-cms/classes/query-filtering/class-wpml-term-display-as-translated-adjust-count.php file.
  2. Look for line 80.
  3. For this query, change wp_ for your database prefix in lines 84, 85, 90, 91 and 93
     $prefix = $this->wpdb->prefix;
    $originalTermCount = (int) $this->wpdb->get_var(
       $this->wpdb->prepare( "
            SELECT
            (
                SELECT term_taxonomy.count
                FROM ".$prefix."term_taxonomy term_taxonomy
                INNER JOIN ".$prefix."icl_translations translations
                    ON translations.element_id = term_taxonomy.term_taxonomy_id
                WHERE translations.trid = icl_t.trid
                AND translations.language_code = %s
            ) as `originalCount`
            FROM ".$prefix."terms AS t
            INNER JOIN ".$prefix."term_taxonomy AS tt
                ON t.term_id = tt.term_id
            LEFT JOIN ".$prefix."icl_translations icl_t
                ON icl_t.element_id = tt.term_taxonomy_id
            WHERE t.term_id = %d
            ",
            $this->sitepress->get_default_language(),
            $term->term_id
        )
    );

14 Responses to “WordPress database error - Table 'wp.wp_terms' doesn't exist”

  1. When making this change, please note there are 5 places in the query that need the prefix modified/fixed.
    wp_term_taxonomy (twice – lines 84 and 91)
    wp_icl_translations (twice – lines 85 and 93)
    wp_terms (line 90)

  2. Betterup just append the correct prefix.


    $prefix = $this->wpdb->prefix;
    $originalTermCount = (int) $this->wpdb->get_var(
    $this->wpdb->prepare( "
    SELECT
    (
    SELECT term_taxonomy.count
    FROM ".$prefix."term_taxonomy term_taxonomy
    INNER JOIN ".$prefix."icl_translations translations
    ON translations.element_id = term_taxonomy.term_taxonomy_id
    WHERE translations.trid = icl_t.trid
    AND translations.language_code = %s
    ) as `originalCount`
    FROM ".$prefix."terms AS t
    INNER JOIN ".$prefix."term_taxonomy AS tt
    ON t.term_id = tt.term_id
    LEFT JOIN ".$prefix."icl_translations icl_t
    ON icl_t.element_id = tt.term_taxonomy_id
    WHERE t.term_id = %d
    ",
    $this->sitepress->get_default_language(),
    $term->term_id
    )
    );

  3. Hi, I’m in trouble here are line 80 to 93
    80 $this->wpdb->prepare( ”
    81 SELECT
    82 (
    83 SELECT term_taxonomy.count
    84 FROM wp_term_taxonomy term_taxonomy
    85 INNER JOIN wp_icl_translations translations
    86 ON translations.element_id = term_taxonomy.term_taxonomy_id
    87 WHERE translations.trid = icl_t.trid
    88 AND translations.language_code = %s
    89 ) as `originalCount`
    90 FROM wp_terms AS t
    91 INNER JOIN wp_term_taxonomy AS tt
    92 ON t.term_id = tt.term_id
    93 LEFT JOIN wp_icl_translations icl_t

    and this is my error log

    fatta da require(‘wp-blog-header.php’), require_once(‘wp-includes/template-loader.php’), include(‘/themes/kallyas/single.php’), get_sidebar, locate_template, load_template, require_once(‘/themes/kallyas/sidebar.php’), dynamic_sidebar, WP_Widget->display_callback, ZN_Widget_Categories->widget, wp_list_categories, get_categories, get_terms, WP_Term_Query->query, WP_Term_Query->get_terms, WP_Term_Query->populate_terms, get_term, apply_filters(‘get_term’), WP_Hook->apply_filters, WPML_Term_Display_As_Translated_Adjust_Count->get_term_adjust_count
    [13-Jul-2022 12:38:31 UTC] WordPress errore sul database Table ‘locandav_wp323.wp_terms’ doesn’t exist per la query

    can you explain how to fix it ? Thanks

    • Hey @lucaC-75,
      According to your error, your database prefix is not “wp_”. Could you please replace it for the one you are using in your WordPress installation as described here?
      Otherwise, you can also use @tonyS-7 approach.
      Finally, you can also open a ticket in our Support forum if any of these alternatives does not fix your issue.
      Regards

  4. Hi!
    This problem arose with the arrival of version 4.5.7, where the class-wpml-term-display-as-translated-adjust-count.php file was introduced. In version 4.5.6 this file did not exist.

    According to the WordPress Code Reference (https://developer.wordpress.org/reference/classes/wpdb/), the correct way to query a table is to use {$wpdb->prefix} instead of the wp_ prefix. Therefore, the problematic query (which goes from lines 81 to 95) should be replaced with:


    SELECT
    (
    SELECT term_taxonomy.count
    FROM {$wpdb->prefix}term_taxonomy term_taxonomy
    INNER JOIN {$wpdb->prefix}icl_translations translations
    ON translations.element_id = term_taxonomy.term_taxonomy_id
    WHERE translations.trid = icl_t.trid
    AND translations.language_code = %s
    ) as `originalCount`
    FROM {$wpdb->prefix}terms AS t
    INNER JOIN {$wpdb->prefix}term_taxonomy AS tt
    ON t.term_id = tt.term_id
    LEFT JOIN {$wpdb->prefix}icl_translations icl_t
    ON icl_t.element_id = tt.term_taxonomy_id
    WHERE t.term_id = %d

    Also, it is recommended to delete the php_errorlog file (if it exists) because it is very likely that it has reached a very large size (more than 200MB)

    Best regards

  5. Does this issue have any apparent affect on anything? I don’t know if I urgently need to implement this fix or wait for it to be fixed in the next release?

    • Hello!
      If you have this error, then you probably have set the fallback mode (as known as Display as translated mode) for categories.
      If your database prefix is different you might get incorrect taxonomy counts for the affected taxonomy.
      So, if the count of the terms and the error (if you have it) is not bothering you, then you can postpone the fix until we release a bugfix release that includes this, however, the fix is pretty trivial and it should be relatively easy to implement – so it is up to you to decide.

      Please remember as a rule of thumb to make backups before doing any changes on a live environment site.

  6. Hey everyone,
    This issue should be solved with our latest release.
    Don’t hesitate to make a full backup before updating your WPML.
    Regards