retrieve post details in loop

时间:2021-05-12 作者:DCEXC

我编写了一个函数来循环浏览用户的帖子,我试图按状态显示每个帖子,并为每个状态组显示不同的消息。

出于某种原因-get\\u post\\u status不起作用,其他任何挂钩也不起作用。有人能帮忙吗?我甚至试着暂时缩短函数,只是回显变量,看看它们在做什么,它只是空白。

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\' );
下面是完整的代码-但是,当我以这种方式编写它时,下面的循环会起作用-但仅适用于“发布”帖子状态。

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
                    }
                
            }

1 个回复
最合适的回答,由SO网友:Jos Faber 整理而成

首先,改变这些====mentioned by @sally-cj 在评论中。

最重要的是,您将状态保存为一个名为$stat 然后检查为$status. 所以你可能想改变$stat = get_post_status($job->ID); 进入$status = get_post_status($job->ID);.

或者更好:the$job 已使用名为post_status, so使用$post->post_status.

相关推荐