繁体   English   中英

Wordpress WP_Query $args,显示已发布的帖子和当前用户的待处理帖子

[英]Wordpress WP_Query $args, show published posts AND the current user's pending posts

在我的 Wordpress 网站上,用户可以在群墙上写自己的帖子。 我想在群墙上显示所有已发布的帖子。 我还想向作为这些帖子作者的当前用户显示待处理的帖子。 现在当用户提交一个新帖子时,没有显示关于它的信息,他们可能不知道他们的帖子是否被提交。 我想向他们展示他们待处理的帖子以及他们需要等待帖子审查的信息。

有没有办法将逻辑添加到WP_Query$args中? 这是我现在的代码:

    //number of posts per page default
    $num =5;
    //page number
    $paged = $_POST['page'] + 1;
    
    $current_user = wp_get_current_user();
    $uid = $current_user->ID;
    
    //args
    $meta_query = array();
    $meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
    $args = array(
        'post_type' => 'pg_groupwalls',
        'post_status' => 'publish',
        'posts_per_page' =>$num,
        'meta_query' => $meta_query,
        'paged'=>$paged
    );
    
    
    
    //query
    $query = new WP_Query($args);
    //check
    if ($query->have_posts()):
        //loop
        while ($query->have_posts()): $query->the_post();

后来我有了我的模板。 这样,它现在只显示已发布的帖子。 我试图在这里找到一些关于它的信息: https://developer.wordpress.org/reference/classes/wp_query/ ,后来我尝试将$args数组更改为如下所示:


    //number of posts per page default
    $num =5;
    //page number
    $paged = $_POST['page'] + 1;
    
    $current_user = wp_get_current_user();
    $uid = $current_user->ID;
    
    //args
    $meta_query = array();
    $meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
    $args = array(
        'post_type' => 'pg_groupwalls',
        'post_status' => array (
             'relation' => 'AND',
             array(
                  'post_status' => 'publish',
             ),
             array(
                  'post_status' => 'pending',
                  'author' => $uid,
             ),
        ),
        'posts_per_page' =>$num,
        'meta_query' => $meta_query,
        'paged'=>$paged
    );
    
    
    
    //query
    $query = new WP_Query($args);
    //check
    if ($query->have_posts()):
        //loop
        while ($query->have_posts()): $query->the_post();

这很可能不是这样做的方法,显然,它不能按我想要的方式工作。 它向所有用户(不仅仅是作为作者的当前用户)显示已发布和待处理的帖子。 我也许可以创建一个新的 WP_Query,但这个已经分页,每页显示 5 个帖子,带有“加载更多”按钮,我不想打破分页。

有没有办法使用$args数组创建这个逻辑? 或任何其他方式来实现我想要的? 先感谢您。

如果需要更多信息或更多代码,我会尝试更新它。

试试下面的代码。

//number of posts per page default
$num = 5;

//page number
$paged = $_POST['page'] + 1;

$current_user = wp_get_current_user();
$uid          = $current_user->ID;

//args
$meta_query   = array();
$meta_query[] = array(
    'key'     => 'pm_accessible_groups',
    'value'   => sprintf(':"%s";',1),
    'compare' => 'like'
);

对于所有发布帖子。

$publish_args = array(
    'post_type'      => 'pg_groupwalls',
    'post_status'    => array( 'publish' ),
    'posts_per_page' => $num,
    'meta_query'     => $meta_query,
    'paged'          => $paged
);

//query
$publish_posts = new WP_Query( $publish_args );

//check
if ( $publish_posts->have_posts() ):
    //loop
    while ($publish_posts->have_posts()): $publish_posts->the_post();

    endwhile;

    wp_reset_postdata();

endif;

对于当前用户的所有待处理帖子。

$pending_args = array(
    'post_type'      => 'pg_groupwalls',
    'post_status'    => array( 'pending' ),
    'post_author'    => $uid,
    'posts_per_page' => $num,
    'meta_query'     => $meta_query,
    'paged'          => $paged
);

//query
$pending_posts = new WP_Query( $pending_args );

//check
if ( $pending_posts->have_posts() ):
    //loop
    while ($pending_posts->have_posts()): $pending_posts->the_post();

    endwhile;

    wp_reset_postdata();

endif;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM