WP_Query
will return a WP_Query
object. An object will never match the string $ip
that you are comparing it to. What you need to be doing is checking whether results were returned, which you can do by checking the found_posts
property:
$ip = $_SERVER['REMOTE_ADDR'];
$query = new WP_Query( array(
'ignore_sticky_posts' => true,
'meta_key' => 'voted_IP',
'meta_value' => $ip
) );
if (0 !== $query->found_posts) {
echo 'one';
} else {
echo 'two';
}
Or you can use the WP_Query
method have_posts
:
$ip = $_SERVER['REMOTE_ADDR'];
$query = new WP_Query( array(
'ignore_sticky_posts' => true,
'meta_key' => 'voted_IP',
'meta_value' => $ip
) );
if ($query->have_posts()) {
echo 'one';
} else {
echo 'two';
}
Note that I added an ignore_sticky_posts
condition. You need that to make this work reliably
By default the query will check for published posts, if you need other post types or post statuses you will need to add more conditions.