I think what you are after is get_post_meta()
and not a custom query.
REASONS:
-
The results from custom fields are cached and extremely streamline when it cone to performance. You can read this answer I have done recently which will explain everything
-
You are trying to get the custom field attached to a post only in a single post page. This is what
get_post_meta()
was designed for
You can replace all your code with something like this: (Modified from the codex)
$key_1_value = get_post_meta( get_the_ID(), 'wpcf-legislacion-gratis', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
EDIT
If you need to get the custom field value outside the loop like in a sidebar or function, you need to replace get_the_ID()
with get_queried_object_id()
$key_1_value = get_post_meta( get_queried_object_id(), 'wpcf-legislacion-gratis', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}