Using two different DB users on one WP install

I have setup WP instance with 2 different database, one for read only and other for admin purpose. But once the admin make the changes, then the ADMIN-DB should be copied and added to SLAVE-DB server. // check if url contains wp-login or wp-admin and then create DB configuration for Master DB else Slave DB. … Read more

WPDB: how to get the value of a field in a custom database table

OK, so first of all, you should always use proper escaping, when building the queries. This is how your code can look after fixing: global $wpdb; $value = $wpdb->get_var( $wpdb->prepare( ” SELECT column_name FROM {$wpdb->prefix}plugin_table WHERE ID = %d “, get_current_user_id() ) ); The things you have to take care of: You have to change … Read more

Two WordPress sites sharing the same database

You won’t be able to use the same database. If you do your going to have all sorts of problems. With the biggest problem being you cannot have two different values for the site URL. I would suggest a managed hosting solution such as Pantheon. This will provide you with a development, test, and live … Read more

Block search SQL from happening

It’s possible to avoid SQL execution in WP_Query with the posts_pre_query filter. It’s e.g. used by plugins to outsource the default search to 3rd party search engines. Here’s an example how we can override the main search query on the front-end, with an empty posts array and avoid running the SQL search query: add_filter( ‘posts_pre_query’, … Read more

Differences between add_post_meta and update_post_meta

This: update_post_meta(42, ‘my_meta_key’, [ ‘my_meta_value_1’, ‘my_meta_value_2’, ‘my_meta_value_3′, ]); Changes a single post meta, but the value is an array, and you can’t put a array in the database, it wants a string for that column. So WordPress serializes it into a string and saves the result. So I have a question, are either of these … Read more

Changing BIGINT to INT

There should not be any downside. The user ID is cast to int in many places in the WP PHP files anyway. But I don’t think this is a real performance problem. If your queries are slow, it might be more useful to inspect each of them and look for opportunities to improve the queries … Read more

Taking WordPress table prefixes into account

You can rewrite the code like this: // setting posts with current date or older to draft if (!wp_next_scheduled(‘sfn_expire_hook’)){ wp_schedule_event( time(), ‘hourly’, ‘sfn_expire_hook’); } add_action( ‘sfn_expire_hook’, ‘sfn_show_expire’); function sfn_show_expire(){ global $wpdb; $server_time = date(‘mdy’); $result = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE post_type=”show” AND post_status=”publish””); if( !empty($result)) foreach ($result as $a){ $show_time = get_the_time(‘mdy’, $a->ID); if … Read more