I'm just starting out on a new Wordpress project and for some reason I can't get the previews working. I have disabled all of my plugins and still i always get false when using is_preview().

Currently I'm just creating a page that renders via page.php where I only call var_dump(is_preview()) and it always shows false. Doesn't matter if I put it in the loop either, still false. What am I missing here?

Shouldn't is_preview() return true when i click "Preview Changes"?

EDIT: I'm of course calling var_dump(is_preview()) and not "the_preview" as i orginially wrote. and it prints bool(false) every time.

RESOLVED: It was actually some misconfiguration in the nginx server running the site. Not sure exactly what it was, it's not my area of expertise so to speak. But the problem is resolved.

  • 1) Just curious, why do you need to use it? 2) Do you put var_dump(is_preview()) in single.phph / page.php ? Can you show us more of the code you are using? – Steven Feb 26 '16 at 10:51
  • I have had to use it in another project to apply the preview filter since I couldn't get it to work any other way when using timber for templating. But since I couldn't get it to work this time I had to try and figure out why and I found that the function never gave me true. Currently I'm calling this from page.php and I'm not doing anything else. So the actual problem is that I cannot get the preview to work. – JimboSlice Feb 26 '16 at 10:58
  • Try a fresh install and use one of the default templates. If it works there, then try adding your own template. – Steven Feb 26 '16 at 11:11

is_preview() should return true is it is a preview. Did you try the following on the page.php?

if(is_preview()){ echo "preview" }else{ echo "no preview" }

to check if the is_preview() function is working?

EDIT2: You could work your way around it and check for:

if($_GET['preview'] == true){}
  • I'm using 4.4.2 and it works just fine here. – Steven Feb 26 '16 at 11:07
  • You're right i was testing it wrong on my installation. – Jorin van Vilsteren Feb 26 '16 at 11:15
  • 1
    Hey guys, it was something wrong in the server configuration. Damn sysadmins! :) Thanks for all your effort! – JimboSlice Feb 26 '16 at 12:51

It does not work "is_preview" because maybe have only activated post with status "publish".

In my case it was for this line. Where only it allows publications that were published.

$query -> set( 'post_status', 'publish' );

you can do the following:

function is_draft() {
    return is_user_logged_in() && isset( $_GET[ 'p' ] ) && ! empty( $_GET[ 'p' ] );
}   // end function

$query -> set( 'post_status', is_draft() ? 'draft' : 'publish' );

There are many solutions but to activate "is_preview" you must have activated the post with status "draft"

Best regards

Gustavo Martínez

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.