Skip to content

Instantly share code, notes, and snippets.

@acki
Last active January 20, 2019 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acki/aca38168baf0970a5ef4b62c6b6173d4 to your computer and use it in GitHub Desktop.
Save acki/aca38168baf0970a5ef4b62c6b6173d4 to your computer and use it in GitHub Desktop.
Helper for saving queries when using ACF in WordPress
<?php
// get field function for ACF without using ACF and generating additional queries (normally)
function get( $selector, $post_id = false, $format_value = true ) {
if( function_exists( 'get_post_meta' ) && function_exists( 'acf_get_valid_post_id' ) ) {
$post_id = acf_get_valid_post_id( $post_id );
// Search the data in post meta
$data = get_post_meta( $post_id, $selector, $format_value );
// If there is no data AND the post_id contains widget, search in option.
// WARNING! This generates queries per request!!
if( $data === false && strpos( $post_id, 'widget' ) !== false )
$data = maybe_unserialize( get_option( $post_id . '_' . $selector ) );
return $data;
} else {
echo '<!-- ERROR get(): ACF not found. Please install. -->';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment