Home › Forums › Front-end Issues › Only users who created a post can edit that post › Reply To: Only users who created a post can edit that post
Hi @roger,
I encountered exactly the same issue, and i’ve written a logic which does the following:
-gets the current logged in user id
-in a loop, get the id’s of all posts by that user
-checks whether this specific post id is created / allowed to be edited by that user
-if allowed, nothing happens, and edit form is loaded /
-if not allowed, exit() and redirect the user
here’s my code, please place it in the page in which your frontend form resides, after your “get_header()” or “wp_head()” (can’t run the loop before that):
$user_ID = get_current_user_id();
$args = array(
'post_type' => 'your_post_type',
'posts_per_page' => -1,
'post_status' => 'any',
'author' => $user_ID
);
// The Query
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
$posts_by_user = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
$posts_by_user[] = get_the_ID();
}
} else {
//echo 'no posts found';
}
/* Restore original Post Data */
wp_reset_postdata();
// Check and redirect if needed
if (in_array($book_ID, $posts_by_user)) {
//echo "Post ID found in array";
} else {
$location = get_home_url();
$status = '302';
wp_redirect( $location, $status );
exit;
}
The code is checked and working, let me know if i can help in any other way.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.