Question

I have written a function to loop through the users posts, and im trying to display each of the posts by their status, with a different message for each status group.

for some reason - get_post_status doesnt work, nor do any of the other hooks. can anyone help? ive even tried to temporarily shorten the function and just echo out the variables to see what theyre doing and its just blank.

function user_item($type, $tax, $term_name, $term_id) {
    if (!is_user_logged_in()) return;  
    $args = array(
        'post_type'             => $type,
        'author'                => get_current_user_id(),
        //'post_status'                 => $status,
        'tax_query' => array(
            array(
            'taxonomy'      => $tax,
            'field'         => $term_name,
            'terms'         => $term_id,
            'include_children'  => false
            )
        )
    );
        
        $jobs = get_posts( $args );
        
            foreach($jobs as $job){
                $jobid = get_the_ID($job);
                $stat =  get_post_status($jobid);
                $items = $job->post_title;
                ?>
                
                <div> <?php echo ($jobid)?></div>;
                <div> <?php echo ($stat)?></div>;
                <div> <?php echo ($items)?></div>;

<?php
            
                    
            }


function fd1(){
    echo user_item('post type name','post taxonomy name','taxonomy term name','taxonomy term id', 'draft');
    }

    add_shortcode( 'fd1_list', 'fd1' );

below is the full code - however the following loop works when i write it this way - but only for the 'publish' post status.

function user_item($type, $tax, $term_name, $term_id, $status) {
    if (!is_user_logged_in()) return;  
    $args = array(
        'post_type'             => $type,
        'author'                => get_current_user_id(),
        'post_status'           => $status, //need to define the status because if left blank it defaults to only publish status
        'tax_query' => array(
            array(
            'taxonomy'      => $tax,
            'field'         => $term_name,
            'terms'         => $term_id,
            'include_children'  => false
            )
        )
    );
        
        $jobs = get_posts( $args );
        
            foreach($jobs as $job){
                $stat =  get_post_status($job->ID);
                $items = $job->post_title;
                
                //echo $items, ' - ', $stat;
                
                    if ($stat === 'publish'){?>
                        <a href="<?php echo get_post_permalink($job ->ID);?>">
                        <div><?php echo $items, ' - ', $status ?></div>
                        </a>
                        <br>
                        <?php
                    }
                    
                    elseif ($stat === 'draft'){?>
                        <a href="<?php echo get_post_permalink($job ->ID);?>">
                        <div><?php echo $items, ' - ', $status ?></div>
                        </a>
                        <br>
                        <?php
                    }
                
                    elseif ($stat === 'pending'){?>
                        <a href="<?php echo get_post_permalink($job ->ID);?>">
                        <div><?php echo $items, ' - ', $status ?></div>
                        </a>
                        <br>
                        <?php
                    }
                
                    elseif ($stat === 'archived'){?>
                        <a href="<?php echo get_post_permalink($job ->ID);?>">
                        <div><?php echo $items, ' - ', $status ?></div>
                        </a>
                        <br>
                        <?php
                    }
                
                    else {?>
                        <a href="<?php echo get_post_permalink($job ->ID);?>">
                        <div><?php echo $items, ' - ', $status ?></div>
                        </a>
                        <br>
                        <?php
                    }
                
            }
Was it helpful?

Solution

First, change those = to === as mentioned by @sally-cj in the comment.

On top of that, you're saving the status as a variable called $stat and are then checking it as $status. So you might want to change $stat = get_post_status($job->ID); into $status = get_post_status($job->ID);.

Or better: the $job is already populated with a field called post_status, so use $post->post_status.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top