'If meta key's value is x then echo

I have 3 pieces of code that essentially should do the same thing, but none of them are working and they are all echoing "Not Working!".

The meta key is wilcity_belongs_to and the value is 2980. These are all based on what plan a listing is on. So if the listing plan is value 2980 then it must echo "Working!".

$meta = get_post_meta( get_the_ID(), 'wilcity_belongs_to',true );
if( $meta == '2980' ) {
    echo "Working!";
} else {
    echo "Not Working!";
};

and

global $wpdb;
$wpdb->get_results( "select * from $wpdb->wpam_postmeta where meta_key = 'wilcity_belongs_to' " );
if ( $wilcity_belongs_to == '2980') {
    echo "Working!";
} else {
    echo "Not Working!";
};

and

if ( get_post_meta( $post->ID, 'wilcity_belongs_to', true ) == '2980' ) {
    echo "Working!";
} else {
    echo "Not Working!";
};

Here is a screenshot of what is in the database: Database This is in Table: wpam_postmeta

Please could i get help on this, ive been trying everything.



Solution 1:[1]

Did you try this way? get_results() usually returns an array with the result set.

global $wpdb;
$result = $wpdb->get_results( "select * from $wpdb->wpam_postmeta where meta_key = 'wilcity_belongs_to' " );
foreach ($result as $row) {
   if ( $row["wilcity_belongs_to"] == '2980') {
       echo "Working!";
   } else {
       echo "Not Working!";
   };
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1