If you look a bit into the source, you’ll see that $wpdb->insert()
will use ->query()
under the hood. So should be the same, right?
Not that simple. It doesn’t just use ->query()
but also ->prepare()
, which is considered best practice. Meanwhile with your code example you’ve probably just opened yourself to SQL injections.
The takeaway here: If it is a simple operation and the ->insert()
etc. method work – use them. They’re tested and contain little risk. Writing your own queries always carries the risk of opening yourself up to troubles such as SQL injections.
Related Posts:
- Connecting to a 2nd DB gives me no results when querying
- wp_usermeta – Read from database, but save function broken
- SQL error with custom query
- Writing a function for WP Cron to run a SQL command daily
- Delete oldest wordpress post (SQL query)
- Function returning queried meta value based on current post ID
- Order posts (across the whole site) by metadata date
- Sort posts by Date (DESC) and by Title (ASC)
- Filter the query ONLY for the search results page
- mysql custom wp query
- Delete post revisions on post publish
- How to sort results from a custom database table
- Most efficient way to get custom database records from 20 buttons and 20 tables?
- How to update BuddyPress xprofile fields programmatically? [closed]
- Change all http to https in mysql databse?
- WooCommerce – Query for ordered products
- How to find if a post with custom_field == X exists?
- WP database error for comments_popup_link()
- Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
- Query WordPress database by registered date and role
- How to get last updated row or ID in wordpress
- Removing the first image in content
- How Do I Merge Categories With phpMyAdmin
- Best Practice for Syncing Local Development With Staging Development [closed]
- greatest() function returns undefined property
- Check if row exists before inserting
- Select Multiple meta_value from WP DB; Single Query
- Remove the deleted users avatar from list
- exclude a category from a search on a specific page
- example of build_query function?
- when I fetch data from remote mysql database in wordpress built in wordpress function is not working?
- CRUD operations using WordPress database API
- WordPress shortcode attributes for database SELECT?
- Shortcode pagination not advancing
- Create WP_Query to search for posts by their categories or their parent/child categories
- Advanced SELECT query with condtional statements
- Dynamic dropdown from database table
- Updating Media Published Date When Parent Post Is Modified in WordPress
- Create a new query in function.php to filter blog posts
- Slow queries constantly getting stuck on WordPress database of ~100,000 posts
- Get User Login Data (date, time… )
- Multisite 404 on pages – rewrite error breaks database
- Accessing two databases wordpress
- Query multiple tables at once?
- Is There Any Built In WP Functionality For Combining Form Fields To Return Specific Data
- How to ignore posts in pre_get_posts?
- Alternative functions for mysql_free_result and mysql_ping in wordpress functions
- Custom WordPress Table wpdb
- Best practice to limit results in get_row()?
- Always get creating a new table with foreign key
- Check if a value exists in database table
- SQL query to set posts in bulk based on the post content
- Query Column of Specific ID from Database Table
- get_terms (or tax_query) for term of current post?
- shorthand syntax for custom fields
- How Do I Delete WordPress Posts Older Than 400 Days, From A WordPress Category
- How to avoid saving empty data to sql while using add_meta_box
- Using One Function To Create Two Different Shortcodes
- How to loop while adding data to database?
- How can I connect to a second database and still be able to use wp functions like get_post_types()?
- Update wp_postmeta table based on 2 keys
- Selecting specific query for offset
- Why isn’t comment validation working?
- WordPress Database Query works in phpMyAdmin but not in the code
- Button click counter for login user
- Function stops working
- Shortcode with pagination advancing multiple queries
- Populate custom post type/custom fields from an external database
- How can I display a query in a page?
- Get multisite count without php?
- WP_Query always returning the last custom post
- Database SQL Error, Should Work
- Database Query not working
- Function to get the name in database table from the comma separated string
- Check if values exists DB
- Sorting date results from a query
- Functions file mods and CPU
- How to Delete Posts by title?
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- Bulk set Post Title as Tag where Tag is Empty Function
- How to trigger an update_option
- Remove posts that start with similar words (like a delete duplicate posts plugin)
- Include posts under a custom taxonomy in author.php
- How to cache results of of get_available_post_statuses() function as it is causing a slow query on a website with over 1 million posts?
- SQL errors when querying for something with apostrophes
- Function wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder
- clickable title of 2 post on mainpage USING functions.php
- Excerpt – First Sentence & Read More
- wp_get_current_user->user login returns %20 for spaces
- Disable every rss feed except home feed
- Notice: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ in ….functions.php on line 73
- How to test for a class (from a plugin) in functions.php
- Query Concatenation
- If two tags in the middle i need comma
- How can I append and prepend something to all post hyperlinks without using ::before or ::after? PHP hook solution?
- Child Theme not working – CSS gone
- WordPress query very slow on +/- 300k DB entries and 7 INNER JOIN
- Show stuff everywhere except single post?
- How to overwrite ‘read more” text for artmag theme
- Call ACF data from functions.php [closed]