Display custom field if it exists and has a specific value [closed]

You can hook into the_title() to alter the output.

In functions.php paste this:

function my_change_title( $title, $post_id ) {
    $custom_title = get_post_meta($post_id, 'mycustomvalue', true);

    if( isset($custom_title) && $custom_title == 'apple' )
        return $custom_title;

    return $title;
}
add_filter('the_title', 'my_change_title', 10, 2);

In your template, you can just use <?php the_title(); ?>


To use your custom value in your template:

<?php $custom_value = get_post_meta(get_the_ID(), 'mycustomvalue', true); ?>
<?php if( isset($custom_value) && $custom_value == "apple" ) : ?>
    Do something
<?php endif; ?>