Last active
January 20, 2019 18:21
-
-
Save acki/aca38168baf0970a5ef4b62c6b6173d4 to your computer and use it in GitHub Desktop.
Helper for saving queries when using ACF in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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