Should close $wpdb via $wpdb->close()

The close() function was added for completeness in the database abstraction. If the database is prematurely closed in a plugin or theme (as you are using it), the next call to wpdb::query() will re-open the connection. Your Commands out of sync error is a MySQL error, not a WordPress one, and it might be coming … Read more

Safe way to find last inserted id in a table?

At a higher level, there truly isn’t any means to know if the php/db connector will return the correct id by relying on $wpdb->insert_id. The only way to be 100% sure is to have add a key you know will be unique (and indexed as such); you can then retrieve the id by querying the … Read more

Creating a Post form outside of the Admin

Ok I have cracked it, by spoofing the $_SERVER variable and pre-defing some constants, I was able to prevent WordPress from redirecting after the inclusion of wp-load.php. define(‘WP_USE_THEMES’, false); define( ‘DOMAIN_CURRENT_SITE’, $siteRow[‘domain’] ); define( ‘PATH_CURRENT_SITE’, “https://wordpress.stackexchange.com/” ); define( ‘SITE_ID_CURRENT_SITE’, 1 ); define( ‘BLOG_ID_CURRENT_SITE’, $siteRow[‘wp_blog_id’] ); $_SERVER = array( “HTTP_HOST” => $siteRow[‘domain’], “SERVER_NAME” => $siteRow[‘domain’], “REQUEST_URI” … Read more

Can’t use get_results() in ajax query

This will not work because you call example.com/markers.php directly, and that file does not load anything from WordPress, like the $wpdb object. You can include wp-load.php, but this might break if you move the WordPress installation to somewhere else. To be more in line with how Ajax calls in WordPress should be made, you should … Read more

Use $wpdb in wordpress theme files

You need to connect your script to WordPress before you can use $wpdb. The usual way to do this is to include wp-blog-header.php: include “/path/to/wordpress/wp-blog-header.php”;

MySQL LIKE not working

Try to replace: AND meta_value LIKE %s with AND meta_value LIKE ‘%%%s%%’ so your SQL will become: AND meta_value LIKE ‘%The%’ instead of: AND meta_value LIKE ‘The’

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