• I’ve created a custom plugin, and link to posts on my main page using permalinks. An example is:
    http://www.mysite.com/post-title/

    However, when I click on that link and the post has a published version and a draft version…my single.php pulls up both of them. Below is my single.php code. Is there somewhere that I can exclude draft posts?

    <?php
    
    get_header();
    
    get_sidebar('left');
    ?>
    
        <div id="content">
    
    	<?php
    	if (have_posts()) : while (have_posts()) : the_post();
    	?>
    
    	<h2 style="margin-bottom:0;"><?php the_title();?></h2>
    	<p style="margin-top:0;"> Author: <?php the_author();?> Date: <?php the_date();?></p>
    
    	<?php the_content(); ?>
    
    	<hr> <?php endwhile; else: ?>
    	<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    	<?php endif; ?>   
    
        </div>  
    
    <?php
    get_sidebar('right');
    
    get_footer();
    
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter space10045

    (@space10045)

    Just to clarify – the two posts (one published and one draft) are separate posts with their own slugs (but the draft has a “-2” at the end of the slug), but have the same post title.

    I would like to reproduce your issue but whenever I am publishing a post from draft, I am not been able to keep the draft version. How are you doing this? How are you keeping both draft and published version of the same post?

    When a published post is reverted back to Draft status WordPress won’t allow a visitor to see the content of that post any more.

    I don’t think there is anything wrong in the code. Can you try to print the post ID on single.php and check what exactly you are getting? Also what var_dump says?

    In the plugin code if you are usingWP_Query, please make sure you have used status=>'publish'.

    If you have other plugins installed in your application, before doing anything else, please deactivate them all and see if the issue still persists. If no, find out the actual plugin by activating plugins one by one and checking whether the issue comes up due to the latest plugin activated.

    Hmm, Separate Posts! That’s what I thought. Anyway, I created two separate posts with same title. Now I have two almost similar permalinks with the difference of having “-2” at the end in one of these two.

    But for me single.php page loads only the published post, as it should be. Also when they are separate posts, there is no way of getting a conflict as those two have completely separate post ID.

    I think you need to look into your plugin code where you are running a query (probably) to fetch the list of posts.

    Thread Starter space10045

    (@space10045)

    Hi – I appreciate your response. What is the proper way to link to a specific post from a listing of posts? Here is the code I used:

    <?php $args = array(
    			'posts_per_page'   => 3,
    			'orderby'          => 'date',
    			'order'            => 'DESC',
    			'post_type'        => 'post',
    			'post_status'      => 'publish',
    			'suppress_filters' => true 
    		);
    	
    		$posts_array = get_posts( $args );
    
    		foreach ( $posts_array as $post ) : setup_postdata( $post );
    		?>
      <a href="<?php echo the_permalink();?>" class="underline"><?php echo the_title();?></a>
        		<?php
    		endforeach; 
    Moderator bcworkz

    (@bcworkz)

    Your query code is fine. I think what Subrata is suggesting is some plugin code is altering post queries, causing drafts to be included in queries. This is easily done through the pre_get_posts action by adding ‘draft’ to the ‘post_status’ query var. If you were to disable all plugins, I think you will find that drafts are no longer included. If you re-activate your plugins one by one, once drafts reappear, the last activated plugin is the one adding ‘draft’ to query vars.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Custom Theme – Single.php pulls up draft posts’ is closed to new replies.