Although I don’t think the number of records in the table should effect the speed of queries significantly, there have been reported issues with WP when doing queries with meta queries. It each post has a significant amount of meta values to get you can also reduce the number of queries using a statement you’re using above
$myvals = get_post_meta(get_the_ID()); which will cause WP to get all values and cache them. Then you can use the function with specific meta_key values without causing a new db query.
Looking at your code.
Have you tried running any part of this without doing the deletes first?
For example, does the query actually return any posts? I’m not sure you can run a query with a meta_value without also including a meta_key. I’d try doing a print_r on the posts to see if there are any.
The next think I notice is that you don’t have a ‘global $post;’ statement. So this would only be able to run in a template file.
The last thing is that with 7500 posts and trying to delete 100K+ meta values this will more than likely time out if loaded in a web page.
You may be better off using $wpdb, but I’m not an expert on $wpdb.
I would also make sure I’m deleting only ACF fields and not all fields with no content as there may be other plugins that depend on those empty records being there.
So, I would get the meta data, then I’d make sure that there is a
'_'.$key that has a value that starts with
'field_' before deleting it.
I would also make sure I’m getting and deleting the right data by echoing stuff out to the screen before I actually did any deleting.
Once I was sure it was going to work I’d probably set this up a s CRON task so that I wouldn’t need to worry about it timing out.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.