Support

Account

Home Forums General Issues ACF fields from WP cached data get_post_meta(), get_option(), get_post()

Unread

ACF fields from WP cached data get_post_meta(), get_option(), get_post()

  • When I am working on my WordPress theme, the loading time increases very much up to 4 seconds, as there are so many queries of ACF functions. In addition, the load time is increased because ACF is loaded at the frontend to interpret the ACF functions.

    However, if you only use WP-own functions, such as get_post_meta(), to call the values ​​from the ACF fields, you can disable ACF on the frontend and retrieve the field data from WP-own functions that are cached.

    This would drastically reduce the loading time. I have worked on an own wrapper functions to call all the ACF field data from WP-own functions, but it’s very hard to handle this.

    CASE 1: It’s easy to handle this, if there are used field groups and fields at one time for one case.

    CASE 2: But … it’s very hard to handle this, if there is a clone field group, which was called more than one time by “get_post_meta()”, “get_option()” and “get_post()->post_content” for ACF blocks at the same page.

    WOW, this needs a lot of conditions to work well.

    Here is an example to call ACF field data by wordpress functions:

    THE WRAPPER FUNCTIONS:

    get_field();

    function WP_GF( $field, $post_id = null ) {	
    	
    	global $post;
    	$post_id = $post->ID;	
    	
    	// Check if parent is ACF block
    	if( $post_id === 'is_block' ) {
    		$get_post = get_post();
    		if( has_blocks( $get_post->post_content ) ) {
    			$blocks = parse_blocks( $get_post->post_content );
    
    			foreach( $blocks as $block ) {	
    				$acf_blockdata = isset( $block['attrs']['data'] ) ? $block['attrs']['data'] : false;
    				if( $acf_blockdata ) {					
    					foreach( $block['attrs']['data'] as $data => $value ) {
    						if( $data === $field ) {
    							$get_field = $value;
    						}
    					}		
    				}
    			}
    		}
    	
    	// Check for ACF option field
    	} elseif( $post_id === 'option' || $post_id === 'options' ) {
    
    		// --> Important to save ACF option fields in wp_load_alloptions(); [ 'autoload' => true ] is necessary for acf_add_options_page() and acf_add_options_sub_page()]
    
    		// Check if field is saved in wp_load_alloptions();
    		$options = wp_load_alloptions();
    		$get_field = isset( $options['options_' . $field] ) ? $options['options_' . $field] : false;
    
    		// If field dosen't exist, get direct option value
    		if( $get_field === false ) {
    			$get_field = get_option('options_' . $field);
    		}	
    		
    		
    	// Get ACF post meta field
    	} else {		
    		$get_field = get_post_meta( $post_id, $field, true );
    	}	
    	
    	if( $get_field ) { 
    		return $get_field; 
    	}
    
    	return false;	
    	
    }

    get_sub_field();

    function WP_GSF( $field, $index, $parent, $type = '' ) {
    	
    	global $post;
    	$post_id = $post->ID;
    	
    	$sub_field_name = $parent . '_' . $index . '_' . $field;
    	
    	// Check if parent is ACF block
    	if( $type === 'is_block' ) {		
    		$get_post = get_post();
    		if( has_blocks( $get_post->post_content ) ) {
    			$blocks = parse_blocks( $get_post->post_content );
    
    			foreach( $blocks as $block ) {	
    				$acf_blockdata = isset( $block['attrs']['data'] ) ? $block['attrs']['data'] : false;	
    				if( $acf_blockdata ) {
    					foreach( $acf_blockdata as $blockdata => $value ) {
    						if( $blockdata === $parent . '_' . $index . '_' . $field ) {							
    							$get_field = $value;							
    						}
    					}
    				}				
    			}
    		}
    	
    	// Check for ACF option field
    	} elseif( $type === 'option' || $type === 'options' ) {
    
    		// --> Important to save ACF option fields in wp_load_alloptions(); [ 'autoload' => true ] is necessary for acf_add_options_page() and acf_add_options_sub_page()]
    
    		// Check if field is saved in wp_load_alloptions();
    		$options = wp_load_alloptions();
    		$get_field = isset( $options['options_' . $sub_field_name] ) ? $options['options_' . $sub_field_name] : false;
    
    		// If field dosen't exist, get direct option value
    		if( $get_field === false ) {
    			$get_field = get_option('options_' . $sub_field_name);
    		}	
    		
    		
    	// Get ACF post meta field
    	} else {
    		
    		$meta = get_post_meta( $post_id );
    		$get_field = isset( $meta["{$sub_field_name}"] ) ? $meta["{$sub_field_name}"] : '';	
    
    		// Check post meta field is array
    		if( is_array( $get_field ) ) {
    			// Get the default value, if no custom value is selected
    			$get_field = $meta["{$sub_field_name}"][0];
    		}
    		
    	}
    	
    	if( $get_field ) { 
    		return $get_field; 
    	}
    
    	return false;
    	
    }

    THE TEMPLATE:

    // FLEXIBLE CONTENT FIELD
    // ******************************************** //
    
    $repeater = isset( $repeater ) ? $repeater : 'content_rows';
    
    if( WP_GF( $repeater ) ) {
    
        foreach( WP_GF( $repeater ) as $i => $row_layout ) {
    		
    		if( $row_layout === 'timeline' ) {
    			$repeater_nested = $repeater . '_' . $i . '_timeline_show';
    			
    			$headline = WP_GSF('timeline_headline', $i, $repeater); 
    			$text = WP_GSF('timeline_text', $i, $repeater);
    			
    			if( $headline || $text ) { ?>
    				<div class="top-content">
    					<?php if( $headline ) {
    						echo '<h2>' . esc_html( $headline ) . '</h2>';
    					}
    
    					if( $text) {
    						echo apply_filters( 'the_content', $text );
    					} ?>
    				</div>
    			<?php } ?>
    
    			<div class="section-content">
    				<?php 
    
    				$block_check = '';
    				if( isset( $block ) && $block ) {
    					$block_check = 'is_block';
    				}
    			
    				$repeater_nested = isset( $repeater_nested ) ? $repeater_nested : 'timeline_show';
    
    				// REPEATER FIELD FROM CLONE
    				// ******************************************** //
    			
    				if( WP_GF( $repeater_nested, $block_check ) ) { ?>
    
    					<div class="timeline">
    						<ul class="timeline-ul">
    
    							<?php for( $i=0; $i < WP_GF( $repeater_nested, $block_check ); $i++ ) {	
    
    								$timeline_year = WP_GSF('timeline_year', $i, $repeater_nested, $block_check);	
    								$timeline_title = WP_GSF('timeline_title', $i, $repeater_nested, $block_check);
    								$timeline_content = WP_GSF('timeline_content', $i, $repeater_nested, $block_check);
    
    								if( $timeline_title || $timeline_content ) { ?>
    
    									<li class="timeline-li in-view">
    										<?php if( $timeline_year ) { ?>
    											<time><?php echo esc_html( $timeline_year ); ?></time> 
    										<?php } ?>
    										<div class="timeline-content">
    											<h3><?php echo esc_html( $timeline_title ); ?></h3>
    											<?php echo wp_kses_post( $timeline_content ); ?>
    										</div>
    									</li>
    
    								<?php }
    
    							} ?>
    
    						</ul>
    					</div>
    
    				<?php } ?>
    																	 
    			</div>
    			
    		<?php }
    		
    	}
    	
    }

    What do you thinks about this idea and how I can optimize this? 🙂

Viewing 1 post (of 1 total)

The topic ‘ACF fields from WP cached data get_post_meta(), get_option(), get_post()’ is closed to new replies.