Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to pull the content from a post/page into a template file.

I'm using the following code, which works fine HOWEVER the content is pulled in regardless of whether it is set to 'live' or 'draft'.

Is there a way of checking to see whether the post is live before pulling in the content?

<?php  show_post('Offers');  // Shows the content of the offers page. ?>

Thank you


Please note this function is being used...

<?php 
function show_post( $path ) {
    $post = get_page_by_path( $path ); 
    $content = apply_filters( 'the_content', $post->post_content ); 
    echo $content;
} 
?>
share|improve this question
    
Are you logged in to the site when testing? WordPress behaves differently in this case depending upon if you are logged in or not. –  Stephen Nov 19 '12 at 14:24
    
Is show_post() a core WordPress function? I don't recognize it. –  Chip Bennett Nov 19 '12 at 14:27
    
Hiya - I did wonder if it was because I was logged in, so I logged out and tested. It does the same thing. –  V Neal Nov 19 '12 at 14:35
    
@ChipBennett, I don't recognize it either. I found a reference to somebody's show_post function online. No idea if it is the same function. –  s_ha_dum Nov 19 '12 at 14:46
add comment

3 Answers

up vote 1 down vote accepted

Your custom function show_post() uses get_page_by_slug() to retrieve the specified page.

Looking at source, it doesn't look like get_page_by_slug() uses post_status. So, you'll need to query $post->post_status within your show_post() function if you want to account for it in the function output:

<?php 
function show_post( $path ) {
    $post = get_page_by_path( $path ); 
    $content = '';
    if ( 'publish' == $post->post_status ) {
        $content = apply_filters( 'the_content', $post->post_content ); 
    }
    echo $content;
} 
?>
share|improve this answer
add comment

I think you should use this function

<?php get_post_status( $ID ) ?>

http://codex.wordpress.org/Function_Reference/get_post_status

Then, you can check the post's status using:

<?php

if ( get_post_status( $ID ) == 'publish' ) {

    echo get_the_title( $ID );
}

?>
share|improve this answer
    
Hiya - I don't want to just get the title, I need to show the content from the post I'm pulling in. I tried get_the_content but it just duplicated the content of the page I was trying to pull the post into. –  V Neal Nov 19 '12 at 14:45
    
Instead of echo get_the_title( $ID ); use $post = get_post($ID); codex.wordpress.org/Function_Reference/get_post –  Mubbashar Nov 20 '12 at 7:04
add comment

When you pull the posts from the database, make sure you set the post_status parameter to publish. Like...

$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 , 'post_status' => 'publish'); $myposts = new WP_Query( $args );

'post_status' => 'publish', is the default if you use get_posts. I'd say this is the best way to do it since you'd be pulling fewer posts from the database.

If for some reason you do have to pull more than one post status, check $post->post_status before printing it.

if ('publish' == $post->post_status) {
    // do stuff
}
share|improve this answer
    
I only need to pull the content from 1 post –  V Neal Nov 19 '12 at 14:40
    
Then only pull a post if it is published. There is no need to check after the fact. What is the query/code that pulls this one post? –  s_ha_dum Nov 19 '12 at 14:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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