WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] check if any custom field with certain name has value

[Resolved] check if any custom field with certain name has value

  • I’m trying to show content, when either one of four custom fields is used.

    I’m trying to show a link to the online shop for a certain record if it’s entered. I have 4 custom fields available for this.

    I have this

    <p class="buy">Buy online:
    	<?php $key = "beatport"; $bietport = get_post_meta($post->ID, $key, true); if ($bietport) { ?><a href="<?php $key="beatport"; echo get_post_meta($post->ID, $key, true); ?>">Beatport</a><?php } ?>
    	<?php $key = "junodownload"; $juno = get_post_meta($post->ID, $key, true); if ($juno) { ?><a href="<?php $key="junodownload"; echo get_post_meta($post->ID, $key, true); ?>">Juno</a><?php } ?>
    	<?php $key = "rushhour"; $rh = get_post_meta($post->ID, $key, true); if ($rh) { ?><a href="<?php $key="rushhour"; echo get_post_meta($post->ID, $key, true); ?>">Rush Hour</a><?php } ?>
    	<?php $key = "discogs"; $dg = get_post_meta($post->ID, $key, true); if ($dg) { ?><a href="<?php $key="discogs"; echo get_post_meta($post->ID, $key, true); ?>">Discogs</a><?php } ?>
    </p>

    I’ve tried this code but doesn’t work. It always gives Yes, even if no values are entered. It works with 1 value, but when more values come into play, it doesn’t anymore…

    <?php
    	$bietport = get_post_meta($post->ID, $beatport, true);
    	$juno = get_post_meta($post->ID, $junodownload, true);
    	$rh = get_post_meta($post->ID, $rushhour, true);
    	$dg = get_post_meta($post->ID, $discogs, true);
    	if ($bietport || $juno || $rh || $dg) { ?>
    		YES
    <?php } else { ?>
    		NO
    <?php } ?>

    The first part should come where it says YES and no will be removed (if I get it working)
    Hoping somebody knows an answer…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Basicy this is the line that causes the error

    if ($bietport || $juno || $rh || $dg) { ?>

    it works if I use

    if ($bietport) { ?>

    but I need to check all fields

    Found my own solution

    if (!$bietport && !$juno && !$rh && !$dg) { ?>

    doesn’t work entirely

    Now I have it 🙂 finally…

    the first if statement was correct. it was above which was incorrect.

    $bietport = get_post_meta($post->ID, $beatport, true);

    should be

    $bietport = get_post_meta($post->ID, beatport, true);

    (without $ sign before key)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] check if any custom field with certain name has value’ is closed to new replies.
Skip to toolbar