0

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
                    }
                
            }
2
  • 1
    if ($status = 'publish'), elseif ($status = 'draft') and elseif ($status = 'draft') - that = should be == or === .. – Sally CJ May 12 at 12:54
  • Sorry, in my previous comment, the other "draft" was a typo and I actually meant "pending".. anyway, I agreed with the current answer and additionally, keep in mind that a shortcode (function) should not echo anything and instead, return the output. Also, the field argument in a tax query clause should be a database field name like term_id or slug, and not the name of a term like "My Category". – Sally CJ May 12 at 18:10
1

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.

2
  • thanks both of your for the replies! i think ive got what you meant. Ive adjusted the code in the thread. it does work. unless theres another change you think i need to make? (im not a dev so this is pretty much the limit of my current skillset) – DCEXC May 13 at 1:48
  • If it works, there is nothing to fix anymore ;-) Glad I could help you – Jos Faber May 13 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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