Need Help to make a logic for editing posts in Frontend

WordPress Development Asked on January 4, 2022

I have an issue, I’m creating classified website,
so I have a login and a registration form and users can post their own adverts.

They have account page where they can see their posted adverts, I’m using WP_Query.
And I want to add possibility to edit these posts.

I’m using wp_update_post() and everything is working except I can’t understand how can I make post ID be dynamic.

Imagine you go to your account page where you can see all adverts posted by you, and you have button "Edit advert", you click on it and go to new page with simple form where you can edit your advert.

Here is my code for my form page template:

Template Name: Edit Post
get_header(); ?>
<main role="main">
    <?php if(is_user_logged_in()) { ?>
        <h3>Edit Post</h3>
        <form id="edit_form" method="post">
            <input type="hidden" name="iseditpost" value="1" />

            <label for="edit_title">Title</label>
            <input type="text" name="edit_title" />

            <label for="edit_content">Sample Content</label>
            <textarea rows="8" name="edit_content"></textarea>

            <input type="submit" value="SUBMIT" name="submitpost" />
    <?php } else { ?>
    <h3>You must be logged in</h3>
    <?php } ?>
<?php get_footer(); ?>

Here my code for editing post:

if(is_user_logged_in()) {

    if(isset($_POST['iseditpost'])) {

        $post_title = $_POST['edit_title'];
        $post_content = $_POST['edit_content'];

        $my_post = array();
        $my_post['ID'] = 350;
        $my_post['post_title'] = $post_title;
        $my_post['post_content'] = $post_content;

        wp_update_post( $my_post );


So as you can see here $my_post['ID'] = 350;, I need 350 to be dynamic, so when user click on button "Edit advert" and redirect to page template with form, post ID must be valid for this advert.

And I can’t find out how to make it.

Sorry for my explanation, if you have any question I will be very glad to try to explain better.
Thanks in advance!

P.S. Don’t look at my validation and sanitization, I will do it later!

One Answer

One option is to pass the post ID as a url parameter when the edit link is clicked.

For example, in some template user can see a list of one's posts. Each post has an edit link and the post id appended to the link as a url parameter.

    <?php foreach( $users_posts as $users_post ) : ?>
            <span><?php echo esc_html( $users_post->post_title ); ?></span>
            <a class="button" href="/edit?id=<?php echo esc_attr( $users_post->ID ); ?>">Edit</a>
    <?php endforeach; ?> 

When the user clicks on one of the edit links s/he is directed to the editing template, where the id parameter is identified and used to determine, if the user can edit the corresponding post.

$post_id = ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? absint( $_GET['id'] ) : 0;
$post_to_edit = $post_id ? get_post( $post_id ) : false;
if ( current_user_can( 'edit_post', $post_id ) && $post_to_edit ) : ?>
    <form id="edit_form" method="post">
            <span>Post title</span><br>
            <input type="text" name="post_title" value="<?php echo esc_attr( $post_to_edit->post_title ); ?>">
        <!-- more form fields -->
        <input type="hidden" name="id" value="<?php echo esc_attr( $post_id ); ?>">
        <!-- nonce field -->
        <!-- submit -->
<?php else : ?>
    <p>Nothing to see here</p>
<?php endif; ?>

You could also do the editing view access checking earlier, for example on template_redirect action.

add_action( 'template_redirect', function(){
    $post_id = ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? absint( $_GET['id'] ) : 0;
    $post_to_edit = $post_id ? get_post( $post_id ) : false;
    if ( ! current_user_can( 'edit_post', $post_id ) || ! $post_to_edit ) {
        wp_safe_redirect( home_url( '/' ) );

Answered by Antti Koskinen on January 4, 2022

Add your own answers!

Related Questions

Add Featured Image and Title to wp_nav_menu items

1  Asked on November 19, 2020 by red5


Google Analytics – add script to functions.php

0  Asked on November 19, 2020 by szachmat


Modify Maximum upload file size text in WordPress Media

0  Asked on November 15, 2020 by tacker


How to display fields from the loop in two separate divs

1  Asked on November 3, 2020 by pual


Correct permalinks and 404-error

1  Asked on November 1, 2020 by vlad


Add custom field to Posts and sort by it

1  Asked on October 27, 2020 by nikname


How to generate the COOKIEHASH from JavaScript

1  Asked on October 26, 2020 by zeth


Custom permalink variable on single post

1  Asked on October 20, 2020 by quyet


get_terms (or tax_query) for term of current post?

2  Asked on October 18, 2020 by fntc


How to customize the Lost Password URL?

0  Asked on October 17, 2020 by radu


How to add one time a new page?

1  Asked on October 17, 2020 by zed93


Woocommerce tables not responsive mobile

1  Asked on October 8, 2020 by rickmorty


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP