Home Forums Add-ons Repeater Field Optimize WP_query (repeater)


Optimize WP_query (repeater)

  • I have a custom post type with a pretty complex repeater (about 50-100 subfields per row).
    My repeater have between 1 and 20 rows on each post.

    Then, I created a WP_Query that retrieves posts from that custom post type, and displays them in a table (1 row of the repeater = 1 row of that table).

    Everything works fine, however the query is quite slow (approx 8 seconds, and it’s only going to get worse). I am currently trying to optimize and reduce the loading time.

    If I am not mistaken, when I do :
    $rows = get_field(‘myrepeater’);
    The query goes to fetch every subfield value in the database, whether I need it for my function or not. Correct? If yes, this is what causes most of the loading time, right?

    Is there any way to prevent that?

    For example, for each row, I first check if a true/false is on true. If on false, I ignore this row:

    $rows = get_field('myrepeater');
    foreach($rows as $row) {
    	$validation = $row['validation'];
    	if($validation != 1) {
    		// Do nothing
    	else {
    		// Get all the required fields and display them in a table row

    Since I don’t need the values of the subfields of that row :
    Is there a way to loop through this subfield, and only if “true” get all the subfields of the row?

  • The best way to optimize the queries it do get all of the meta data in a single query rather then getting the specific repeater fields. using

    $all_field = get_fields($post_id);

    This causes a single query to be done that loads all of the values to be retrieved and cached and it is faster than getting the repeater because getting just the repeater causes multiple queries to be done. It causes ACF to loop over each row of the repeater an does a separate query for each sub field of each row.

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

You must be logged in to reply to this topic.