Search code examples
wordpresspostcodex

How to check if a post id is different in WordPress


In order to check if a post id exist, i do this :

if ('publish' == get_post_status ($post_id)) {
    // do something
}

But how can i check if the id is different like in php :

if ($id != $var) {
    //do stuff
}

What i am doing :

$post_id = get_the_ID();
if ($post_id != 835) {
    //do stuff
}

Is it correct ?


Solution

  • Using either of the following should work for you then. If the current post id is not 8 then print out "Not post 8". You can add an else statement if you want something else printed.

    <?php
        $post_id = get_the_ID();
        if ($post_id != "8") {
            echo "Not post 8";
        }
    ?>
    
    <?php
        $post_id = get_the_ID();
        if ($post_id != 8) {
            echo "Not post 8";
        }
    ?>