delete in wordpress query
To delete in a WordPress query, you can use the following code:
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->postmeta WHERE post_id = %d",
$post_id
)
);
Explanation:
1. global $wpdb;
- This line allows you to access the WordPress database functions using the global $wpdb
variable.
2. $wpdb->query()
- This function performs a MySQL database query.
3. $wpdb->prepare()
- This function prepares a SQL query for safe execution to prevent SQL injection.
4. "DELETE FROM $wpdb->postmeta WHERE post_id = %d"
- This is the SQL query that will delete the data from the database table postmeta
where the post_id
matches the provided ID.
5. $post_id
- This variable should contain the ID of the post you want to delete.
Make sure to replace $post_id
with the actual post ID you want to delete.