Support

Account

Home Forums General Issues Move ACF field to separate db table

Helping

Move ACF field to separate db table

  • Hi all,

    I would like to speed up my website. All ACF settings are stored in wp_postmeta which is very inefficient in my use case. I got about 400 000 entries in wp_postmeta and quick searching operations are not possible. Therfore I would like to move the data in separate database tables.
    What I did so far:

    • 1. Creating individual new wordpress table wp_test (post_id, field1, field2, …)
    • 2. add_filter( ‘acf/update_value’, ‘my_disregard_acf_fields’, 10, 3 ); to prevent saving fields (field1, field2, …) into wp_postmeta
    • 3. add_action(‘acf/save_post’, ‘my_save_function’, 1); to save selected fields ([post_id], field1, field2, …) into wp_test

    This works perfectly but I am still looking for an efficient solution to load the content into standard methodes (like get_field()) from ACF. Sure I can use add_filter( ‘acf/load_value’, ‘my_load_function’, 11, 3 ); to fetch the field requests (get_field(field1), get_field(field2), …) and redirect them to the right field in wp_test. But doing this I will have far too much db query requests which is inefficient too.
    The better way would be to have one DB query which requests all relevant fields by post_id and match them to the right get_field() values. Does anyone have an idea how this could work?

    To be honest I do not know how exaclty WordPress works at this point. But I assume that once all wp_postmeta content for the corresponding post_id is loaded from the database into one big object. ACF gets the relevant data from there. Now I would only have to add my data into this object, then ACF should continue to work normally. Does anyone have an idea?

    Or does someone has a totally different idea to make ACF faster?

    Thanks for every hint in advance!

    Greetings Andre

  • I can’t give you any advice on using a different table, but since you mention that you don’t know how WP works I will give you some insight into it.

    When you are in “The Loop”

    
    while (have_posts()) {
      the_post();
    }
    

    part of what WP does automatically is that it gets all of the post meta values for the current post in a single query, or it should, and stores all of the values in the meta cache for the post. Queries for individual post meta values should not be made in this case.

    When you are getting values for other posts when not in a loop and WP is not automatically setting up the post and getting the meta values then it is possible that WP will do a query for each meta value. However, there is a solution for this.

    
    get_post_meta($post_id);
    

    When you call get_post_meta() with a post ID and no meta key specified then WP gets all of the post meta values for the post in a single query and stores the values in the cache so that later it will not, or should not, perform individual queries for each meta value.

    If you still want to go with custom tables then you might want to look at this https://www.awesomeacf.com/storing-acf-data-custom-database-tables/

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.