On my Wordpress website users can write their own posts on the group wall. 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
? 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:
//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.
Is there a way to create this logic using $args
array? 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.