简体   繁体   English

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

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

On my Wordpress website users can write their own posts on the group wall.在我的 Wordpress 网站上,用户可以在群墙上写自己的帖子。 I want to display all published posts on the group wall.我想在群墙上显示所有已发布的帖子。 I also want to display pending posts to the current user who is the author of these posts.我还想向作为这些帖子作者的当前用户显示待处理的帖子。 Right now when the user submits a new post, there is no information displayed about it, they may not know if their post got submitted.现在当用户提交一个新帖子时,没有显示关于它的信息,他们可能不知道他们的帖子是否被提交。 I want to show them their pending post with the information that they need to wait for the post review.我想向他们展示他们待处理的帖子以及他们需要等待帖子审查的信息。

Is there a way to add the logic to the $args of the WP_Query ?有没有办法将逻辑添加到WP_Query$args中? This is my code right now:这是我现在的代码:

    //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();

And later I have my template.后来我有了我的模板。 This way it is only showing published posts as of now.这样,它现在只显示已发布的帖子。 I tried to find some information about it here: https://developer.wordpress.org/reference/classes/wp_query/ , and later I tried changing the $args array to something like this:我试图在这里找到一些关于它的信息: 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();

It is most probably not the way to do this, and obviously, it does not work the way I want.这很可能不是这样做的方法,显然,它不能按我想要的方式工作。 It shows both published and pending posts to all users (not just the current user who is an author).它向所有用户(不仅仅是作为作者的当前用户)显示已发布和待处理的帖子。 I could maybe create a new WP_Query, but this one is already paginated and showing 5 posts per page with a "Load More" button, I don't want to break the pagination.我也许可以创建一个新的 WP_Query,但这个已经分页,每页显示 5 个帖子,带有“加载更多”按钮,我不想打破分页。

Is there a way to create this logic using $args array?有没有办法使用$args数组创建这个逻辑? Or any other way to achieve what I want?或任何其他方式来实现我想要的? Thank you in advance.先感谢您。

If there is more information needed or more of the code, I will try to update it.如果需要更多信息或更多代码,我会尝试更新它。

Try below code.试试下面的代码。

//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'
);

For all publish post.对于所有发布帖子。

$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;

For all pending posts of current user.对于当前用户的所有待处理帖子。

$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