Ask your WordPress questions! Pay money and get answers fast! (more info)

Compare two custom fields WordPress

  • SOLVED

Hi!

I'm having two custom fields, price and regular price. Sometimes both are populated with a value, sometimes only the price field. Sometimes the values are the same, sometimes different.

I would like to check if both have values, and if they do, I would like to compare them. If price is <strong>lower</strong> than regular price, I would like to display it, otherwise just keep it hidden.

How can I do this?

Thanks!

// Jens.

Answers (1)

2013-05-06

Yakir Sitbon answers:

That's very simple "IF" line..
Are you developer?

You just need get two custom fields and check it.

What's the problem?


Jens Filipsson comments:

Well, why do you think I'm asking on this site? If you don't want to help me, then please don't waste my time... I do have the two custom fields, but I do not know how to check it...


Yakir Sitbon comments:

Sorry, test this code..

$regular_price = get_post_meta( get_the_ID(), 'regular_price', true );
$price = get_post_meta( get_the_ID(), 'price', true );

if ( empty( $price ) && empty( $regular_price ) )
echo 'No found price';
else if ( ! empty( $price ) && ! empty( $regular_price ) ) {
if ( $price < $regular_price )
echo $regular_price;
else
echo $price;
}


Yakir Sitbon comments:

$regular_price = get_post_meta( get_the_ID(), 'regular_price', true );
$price = get_post_meta( get_the_ID(), 'price', true );

if ( empty( $price ) && empty( $regular_price ) )
echo 'No found price';
else if ( ! empty( $price ) && ! empty( $regular_price ) )
if ( $price < $regular_price )
echo $regular_price;
else
echo $price;
else if ( ! empty( $price ) )
echo $price;
else if ( ! empty( $regular_price ) )
echo $regular_price;


fix some thing..


Jens Filipsson comments:

Thanks, works like a charm!