$wpdb can not pass Plugin Check (PCP)

  • Unknown's avatar

    This is how I am trying to fetch data from WordPress table, but Plugin Check (PCP) always showing the below errors
    WordPress.DB.PreparedSQL.NotPrepared
    WordPress.DB.DirectDatabaseQuery.DirectQuery
    WordPress.DB.DirectDatabaseQuery.NoCaching

    here is my code

    global $wpdb;

    // Example variable for demonstration

    $some_var = ‘example_value’; // Ensure this is properly sanitized or validated before use

    // Define a unique cache key based on the query

    $cache_key = ‘my_table_data_’ . md5($some_var);

    // Try to get the cached results

    $results = wp_cache_get($cache_key);

    if ($results === false) {

        // Cache miss: Properly prepare the SQL query

        $sql = $wpdb->prepare(

            “SELECT * FROM {$wpdb->prefix}table_name WHERE column_name = %s”, // Note: Table name does not need to be prepared, column name does

            $some_var // The dynamic value that needs preparing

        );

        // Execute the prepared query using get_results()

        $results = $wpdb->get_results($sql);

        // Check for any database errors

        if ($wpdb->last_error) {

            error_log($wpdb->last_error); // Log any database error

        }

        // Store the results in cache for future use, with a 1-hour expiration (3600 seconds)

        wp_cache_set($cache_key, $results, ”, 3600); // Cache for 1 hour

    }

  • Hi there,

    Can you please let me know your site’s address so I can point you in the right direction of getting support?

    Please note that these forums are for WordPress.com hosted sites only. If your site is using the open-source WordPress.org software you need to seek help at these forums:

    https://wordpress.org/support/forums/

  • The topic ‘$wpdb can not pass Plugin Check (PCP)’ is closed to new replies.