0

I am trying to show the front end user only the posts they created. example.com/all-releases

Like User-1 can only see his posts, not the User-2 posts nor admin posts.

I tried various function codes from the previous question but the latest WordPress version does not support any.

Let me tell you the situation: The user is a Logged In user. The posts are created by Gravity Forms.

1
<?php
    $query = new WP_Query([
        'author' => get_current_user_id(),
    ]);

while ( $query->have_posts() ) {
    $query->the_post();
    the_title();
}
    wp_reset_postdata();
?>

In the example above we are using WP_Query look on the reference guide for additional parameters such as different post types like if you want to get only pages, posts or custom post types, as well as the number of elements to be displayed.

Also be aware of the usage of wp_reset_postdata as the example above uses $query->the_post()

Note: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.`

You can also break the code above to add additional HTML tags in between each like:

while( $query->have_posts() ) : $query->the_post(); 
?>
<h2><?php the_title(); ?><h2>
<div><?php the_content(); ?>
<?php endwhile; wp_reset_postdata(); ?>

The example above uses get_current_user_id which assumes there's a user logged in as it returns zero if no user is logged in you can use some logic around that to display different data in that scenario.

  • Hi there, thanks for the answer. I’m not actually a developer, I’m using multiple plugins to create cms for my business. I stuck at the User Only Posts thing, rest is going great. I would really appreciate if you can guide me step by step. As I am just using Googling for all the errors & help. – Aman Sep 29 at 16:38
  • Sure, what theme are you using and where you want to display this. – Crisoforo Gaspar Sep 30 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.