【问题标题】:Wordpress WP_Query $args, show published posts AND the current user's pending postsWordpress WP_Query $args,显示已发布的帖子和当前用户的待处理帖子
【发布时间】:2021-06-30 01:47:54
【问题描述】:

在我的 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 数组创建这个逻辑?或任何其他方式来实现我想要的? 提前谢谢你。

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

【问题讨论】:

    标签: php arrays wordpress posts args


    【解决方案1】:

    试试下面的代码。

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

    【讨论】:

    • 感谢您的回答,但这不会只显示当前用户的帖子吗?我想显示所有用户的已发布帖子,但也只显示当前用户的待处理帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多