I have this simple piece of code that is not working:

    if ( 'publish' != $post->post_status ) {

When I use this in my code, it blocks posts that should have a status of 'publish'.

If I replace this with:

    if ( 'publish' != get_post_status( $post_id) ) {

It works, however this is working on the post status from the last save, not the current edit activity.

I've tried all sorts of combinations alternatives, but I still can't get it work. Any ideas what I may be doing wrong?

  • 1
    Where do you put this code? Are you sure that $post variable is set? – Krzysiek Dróżdż Jan 30 '18 at 10:57
  • Ah I fixed this almost as soon as I posted the question. The problem was with my $post variable, I was not creating it properly. Thanks for your interest. – Venutius Jan 30 '18 at 11:01

Do you have global $post; above the if ('publish... statement?

Reference https://codex.wordpress.org/Function_Reference/$post

  • Yep this was missing, unfortunately – Venutius Jan 30 '18 at 11:26

The problem in my code was down to the $post variable, I'd not declared it properly, I added $post=get_post($post_id); to my code and all worked as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.