Support

Account

Home Forums General Issues Bulk Fetch ACF field across multiple posts

Solved

Bulk Fetch ACF field across multiple posts

  • I am looking to populate a map. I want to build the data for populating the map using data from vanilla post types, lat lng values from the ACF google maps and a few other ACF field values.

    So step 1 is to obviously grab all the posts that have the location ACF field present.

    $gmaps_data = function() {
        $all_locations = get_posts([
            'numberposts' => -1,
            'exclude' => [get_the_id()],
            'meta_key' => 'location'
        ]);
    

    but get_posts() doesn’t give me access to the ACF field data itself. Now the normal way to do this would be to then loop through each posts and do a bunch of get_fields() requests for every post.

    But wouldn’t that mean opening a connection to the database, making the query and closing the connection dozens of times? That sounds horribly inefficient. Why make dozens of tiny queries when I could just make one medium sized one pull all the meta_keys I need for everything then match stuff up serverside. Is this possible? Is there some weird WordPress voodoo going on that somehow makes multiple quickfire get_fields() queries not a horrible idea?

  • There isn’t any way to do this using WP_Query() or get_posts().

    To to this your would need to query the DB directly using $wpdb

    A quick example of a basic query to get meta from published posts. This is not perfect, just an example of what needs doing

    
    SELECT wp_posts.ID, wp_postmeta.meta_value
    FROM wp_posts, wp_postmeta
    WHERE wp_posts.post_status = "publish" AND
          wp_posts.ID = wp_post_meta.post_id AND
          wp_postmeta.meta_key = "location"
    
  • Sort of feared as much. Not ACF’s fault. Sort of defeats the purpose of an ORM if you spend half your time manually querying the DB.

  • @lfleming

    How many posts do you need to query exactly?

    What if you look to store the post IDs elsewhere, saving the need to run the query each time?

    One possible option would be to use rest API and add some ACF custom fields. Whenever a post is updated or published, you could then look to run a function which runs a batch process script, gathering all the post IDs or other data and saves that to a specific field.

    That way, your map only needs to use this data rather than constantly running queries.

    I found querying from the API far quicker, especially when running as a batch processing script.

  • I do something along the lines of what @jarvis suggests, but forgot about it. I have done something like this is the past. In my case I collect a lot of information from posts and store the collected information in the wp_options table. Basically I have a options value that is an array

    
    array(
      $post_id => array(
        // nested array contains data for post
      )
    )
    

    This is updated whenever a post is saved. So that instead of doing a query to get the posts I just get the option value.

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

You must be logged in to reply to this topic.