Question

I'm showing published and future posts on a category page. I need to make future posts unclickable and add a special class to them in order to add a gray overlay, meaning the post is upcoming.. trying something like

$post_status = get_post_status($post_id);
            if($post_status === 'future')

doen't work at all..How can I, inside the same loop, disable a link and add a class if post status is "future" ??

any help would be appreciated ..

Here is the first part of my loop (it's a premium theme, and I'm building a custom category page in a child theme)

$args = array(
    'post_type'=> 'post',
    'post_status' => array('publish','future'),
    'showposts' => -1,
        'category'          => get_queried_object_id(), // Current category id
        'category__in'      => array( get_queried_object_id() )
    );



$query= new WP_Query($args);
if ($query->have_posts()) :
    while ( $query->have_posts() ) : $query->the_post();

        $counter++;?>
        <?php  if( $counter % 2 == 0 ) :     
            if ('future'===get_post_status() ){
            echo '<div class="td-pb-row future">';
        }else if('publish'===get_post_status() ){
            echo '<div class="td-pb-row">' ;
        } ?>                    
            <div class="td-pb-span6">
                <h3 class="entry-title td-module-title">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute() ?>">
                        <?php the_title() ?>
                    </a>

                </h3>
                <?php the_content();?>
            </div>

            <div class="td-module-image td-pb-span6">
                <div class="td-module-thumb">
                    <?php
                    if ( current_user_can('edit_published_posts') ) {
                        edit_post_link('Edit', '', '', '', 'td-admin-edit');
                    }
                    ?>
                    <a href="<?php the_permalink() ?>" rel="bookmark" class="td-image-wrap" title="<?php    the_title_attribute() ?>">
                        <?php   $post_thumbnail_url = '';
                        if( get_the_post_thumbnail_url(null, 'medium_large') != false ) {
                            $post_thumbnail_url = get_the_post_thumbnail_url(null, 'medium_large');
                        } else {
                            $post_thumbnail_url = get_template_directory_uri() . '/images/no-thumb/medium_large.png';
                        }
                        ?>
                        <img class="entry-thumb" src="<?php echo esc_url($post_thumbnail_url) ?>" alt="<?php the_title() ?>" title="<?php echo esc_attr(strip_tags(the_title())) ?>" />
                    </a>
                </div>
            </div>
Was it helpful?

Solution

Just use the function get_post_status(). Inside the loop you have access to the Post Id, but if you leave this blank it will use the ID of the current post instead.

So you code will become something like this (not tested yet):

<h3 class="entry-title td-module-title">
    <?php if( get_post_status() != 'future' ) { ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute() ?>">
        <?php the_title() ?>
    </a>
    <?php } else { ?>
        <?php the_title() ?>
    <?php } ?>
</h3>

The problem in your code might be with the $counter variable. The check for post status 'future' is only done for half of the posts, not for all of them, due to the % 2. So I think you should get rid of this $counter variable.

So to give an example of how you should change your code:

<?php if( 'future' == get_post_status() ){
    echo '<div class="td-pb-row future">';
} else {
    echo '<div class="td-pb-row">';
} ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top