0

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>
4
  • how did you set $post_id?
    – Michael
    Apr 13, 2021 at 0:09
  • I grab the code somewhere !! ;)..In fact I don't know how put condition on post status.whithin a wp_Query loop..
    – blogob
    Apr 13, 2021 at 0:18
  • in this case, please post the full code of your WP_Query loop
    – Michael
    Apr 13, 2021 at 2:29
  • yes thank you Michael, I just edited my question
    – blogob
    Apr 13, 2021 at 8:20

1 Answer 1

2

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">';
} ?>
3
  • thank you very much leendertvb, in fact the modulo is here to display alternate post (once image on left, once on the right)..I'll try your code and give a feedback
    – blogob
    Apr 13, 2021 at 9:15
  • It worked, thank you very much leendertvb: !! you really helped me !
    – blogob
    Apr 13, 2021 at 11:13
  • Great to hear, Good luck!
    – leendertvb
    Apr 13, 2021 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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