store custom WP table names in a global variable

The DB_ constant prefix in WordPress is generally considered reserved for DB_NAME, DB_HOST, DB_USER and DB_PASS. Using it for plugin-specific constants is, in my opinion, not a great idea. The only implication it might pose is if other plugins try to use the constants, but that’s purely theoretical. The proper way to do this is … Read more

How to get top 10 user is based on user meta value

I guess you can do with get_users, something like: $topusers = get_users( array ( ‘meta_key’ => ‘todayuserclicks’, ‘meta_value’ => ”, ‘meta_compare’ => ‘!=’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘DESC’, ‘number’ => ’10’, ) );

Activation hook not creating table

The table name needs to be in quotes on the line where you’re checking if it exists. I tested it, and after that was fixed, the table was successfully created. Here is the full code: function mm_booking_activate() { global $wpdb; $mm_booking_table_name = $wpdb->prefix . “mm_booking”; $mm_booking_db_version = ‘1.0’; $charset_collate = $wpdb->get_charset_collate(); if ( $wpdb->get_var( “SHOW … Read more

Get random row from custom table

[Note: @marwyk87 posted his answer while I was composing this, which represents another way to fix the problem] You’ve got a simple syntax error in your SQL, because of the way you’re referencing the table name. You should just say $sql = $wpdb->prepare( ” SELECT * FROM {$wpdb->prefix}quotes ORDER BY RAND() LIMIT 1 “); and … Read more

$wpdb not returning data

Here’s your problem: if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $result is not a mysqli_result object, it’s literally the results of the query. Since you never passed a second parameter to the get_results method specifying what format you wanted them in, it’ll assume “OBJECT”, so the correct … Read more

Get WooCommerce product attribute taxonomies in a SQL query on WordPress database

With WordPress to make a custom SQL query, you should always use the dedicated WPDB Class and related methods as get_results(), this way: global $wpdb; $results = $wpdb->get_results( ” SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies ” ); if ( count($results) > 0) { $data = []; // Initializing // Loop through results objects foreach( $results as $result … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)