How to get INSERT errors from $wpdb?

To insert data you should use query() (documentation), get_var() method is for selecting data. If error is encountered, query() returns FALSE. query() This function returns an integer value indicating the number of rows affected/selected for SELECT, INSERT, DELETE, UPDATE, etc. For CREATE, ALTER, TRUNCATE and DROP SQL statements, (which affect whole tables instead of specific … Read more

WordPress insert query is not working : Showing no Error

There appears to be a syntax error on line 4. string is not quoted ↓ ↓ ↓ $sql = “CREATE TABLE IF NOT EXISTS ” . $wpdb->prefix.credofy_contact_form. ” ( Instead, I would recommend moving your table creation logic to a plugin and having it run off an installation hook. Below is an example: <?php /** … Read more

Pages from custom table

Your code worked absolutely fine for me, so I suspected you forgot to flush the rewrite rules. Each time after you added code which alters the WordPress rewrite rules, e.g. add_rewrite_rule() or code which registers/un-registers a CPT or custom taxonomy with URL rewriting enabled, you should always flush the rewrite rules which means WordPress will … Read more

Connect to database in the header of my WordPress website

You can (and should) use the existing database connection by accessing global $wpdb. The simplest way to use it is with the get_results() method: global $wpdb; $results = $wpdb->get_results( ‘SELECT * FROM my_table’ ); There’s other methods available, and things you need to consider like preparing queries and using the correct table prefix (if your … Read more

Update Custom Post Type Taxonomies with SQL

Why am I getting this error? Because the table wp_term_taxonomy does not have the column object_id. And the correct table that you should update is the wp_term_relationships table which does have the column object_id. See the WordPress database diagram and also the WordPress database description for more details. So your SQL query should be: UPDATE … Read more

Replace a character in all post titles and slugs

Slugs are saved in the very same table but in the post_name column. So your query would look like this: UPDATE wp_posts SET post_name = REPLACE(post_name, ‘m’ , ‘p’) WHERE post_type=”post” AND post_status=”publish”; By the way, I’d suggest you to use $wpdb->posts instead of just wp_posts ( then it would be compatible with different prefixes, … Read more