Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Frontend form with multiple posts

What was happening was, the global variable $post wasn’t ending up on the POST page – so in the conditional below where the form was being processes,

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['postcontent']) ) {

        header('Location: /');

        $post_id = $post->ID;
        $post = array(
            'ID'             => esc_sql($post_id),
            'post_content'   => esc_sql($_POST['postcontent']),
            'post_title'     => esc_sql($_POST['post_title'])
        );
        wp_update_post($post);

        if ( isset($_POST['owner_name']) ) update_post_meta($post_id, 'owner_name', esc_sql($_POST['owner_name']) );
        if ( isset($_POST['phone']) ) update_post_meta($post_id, 'phone', true );
        if ( isset($_POST['address']) ) update_post_meta($post_id, 'address', true );
        if ( isset($_POST['zip_code']) ) update_post_meta($post_id, 'zip_code', true );
        if ( isset($_POST['add_coupon']) ) update_post_meta($post_id, 'add_coupon', true );
    }

$post_id evaluated to null.

My solution was to get the ID from the $_POST object. Here’s the revised code:

    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['postcontent']) ) {

            header('Location: /');

            $post_id = $post->ID;
            $post = array(
                'ID'             => $_POST['post_id'], // use $_POST object instead of global $post
                'post_content'   => esc_sql($_POST['postcontent']),
                'post_title'     => esc_sql($_POST['post_title'])
            );

            wp_update_post($post);


            if ( isset($_POST['owner_name']) ) update_post_meta($_POST['post_id'], 'owner_name', esc_sql($_POST['owner_name']) );
            if ( isset($_POST['phone']) ) update_post_meta($_POST['post_id'], 'phone', esc_sql($_POST['phone']) );
            if ( isset($_POST['address']) ) update_post_meta($_POST['post_id'], 'address', esc_sql($_POST['address']) );
            if ( isset($_POST['zip_code']) ) update_post_meta($_POST['post_id'], 'zip_code', esc_sql($_POST['zip_code']) );
            if ( isset($_POST['add_coupon']) ) update_post_meta($_POST['post_id'], 'add_coupon', esc_sql($_POST['add_coupon']) );
        }

And the full page for reference

<?php
/**
 * Template Name: Edit Post
 *
 * This is the default template.  It is used when a more specific template can't be found to display
 * posts.  It is unlikely that this template will ever be used, but there may be rare cases.
 */
get_header(); // Loads the header.php template.

do_action( 'before_content' ); // supreme_before_content
do_action( 'templ_before_container_breadcrumb' );

?>
<title>Edit your classifieds</title>
<section id="content" class="large-9 small-12 columns">
<?php do_action( 'open_content' ); ?>

<?php
$user = wp_get_current_user()->data;
$user_id = $user->ID;

$query = new WP_Query(array(
    'post_type' => 'classified',
    'post_status' => 'draft',
    'author' => $user_id,
    // 'posts_per_page' => -1
));
echo "<h1>Edit your pending classifieds</h1><br>";
echo "<p>The following posts are your pending classifieds. After you edit a classified, it will be reviewed by our team before publishing.</p>";
?>
<div class="accordion" id="post-listing"><?php
    while ($query->have_posts()) {

        $query->the_post();

        // process form
        // if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['postcontent']) ) {

            // header('Location: /');

            $post_id = $post->ID;
            $post = array(
                'ID'             => $_POST['post_id'],
                'post_content'   => esc_sql($_POST['postcontent']),
                'post_title'     => esc_sql($_POST['post_title'])
            );

            wp_update_post($post);
            // var_dump($post);
            // var_dump($post_id);

            if ( isset($_POST['owner_name']) ) update_post_meta($_POST['post_id'], 'owner_name', esc_sql($_POST['owner_name']) );
            if ( isset($_POST['phone']) ) update_post_meta($_POST['post_id'], 'phone', esc_sql($_POST['phone']) );
            if ( isset($_POST['address']) ) update_post_meta($_POST['post_id'], 'address', esc_sql($_POST['address']) );
            if ( isset($_POST['zip_code']) ) update_post_meta($_POST['post_id'], 'zip_code', esc_sql($_POST['zip_code']) );
            if ( isset($_POST['add_coupon']) ) update_post_meta($_POST['post_id'], 'add_coupon', esc_sql($_POST['add_coupon']) );
        }


        wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' );


        $fields = get_post_meta($post->ID);

        $name    = $fields["owner_name"][0];
        $phone   = $fields["phone"][0];
        $address = $fields["address"][0];
        $zip     = $fields["zip_code"][0];
        $coupon  = $fields["add_coupon"][0];
        ?>
        <!-- <pre><?// php var_dump($post->ID);?></pre> -->
        <div class="accordion-navigation step-wrapper">
            <a class="step-heading" href="#" onclick="toggle(edit_post_<?php echo $post->ID; ?>);"><span style="float: none;max-width: unset;text-align: left;padding-left: 26px;"><h4 style="margin: 0">Edit <?php echo $post->post_title; ?></h4></span></a>
            <div class="step-post content  clearfix" id="edit_post_<?php echo $post->ID; ?>" href="#">
                <div class="step-post content  clearfix current">
                    <form id="post" class="dropzone form_front_style" method="post" enctype="multipart/form-data">

                    <input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Title</label>
                        <input class="textfield" type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></div>

                        <h3>Classified Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="postcontent">Detail</label>
                        <span style="color: #333"><?php wp_editor( $post->post_content, 'postcontent' ); ?></span></div>

                        <h3>Seller Contact Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="name">Owner Name</label>
                        <input class="textfield" type="text" id="name" name="owner_name" value="<?php echo $name; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="phone">Phone</label>
                        <input class="textfield" type="text" id="phone" name="phone" value="<?php echo $phone; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="address">Address</label>
                        <input class="textfield" type="text" id="address" name="address" value="<?php echo $address; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="zip_code">Zip</label>
                        <input class="textfield" type="text" id="zip_code" name="zip_code" value="<?php echo $zip; ?>" /></div>

                        <h3>Coupons</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="add_coupon">Add Coupon</label>
                        <input class="textfield" <?php echo $coupon ? "disabled style="background-color:#f8f8f8;color:#bdbcbc;"" : ""; ?> type="text" id="add_coupon" name="add_coupon" value="<?php echo $coupon; ?>" /></div>

                        <input class="textfield" type="submit" id="submit" value="Update" />
                    </div>
                </form>
            </div>
        </div>
        <?php
        }
        ?>
    </div>
</section>
<!-- #content -->
<?php do_action( 'after_content' );
$page_for_posts = get_option( 'page_for_posts' );

if ( $page_for_posts != '' ) {
    apply_filters( 'supreme-post-listing-sidebar',supreme_post_listing_sidebar() );// load the side bar of listing page
} else {    
    get_sidebar();
}
get_footer();
?>
<script>
// open accordion when anchor link directly above is clicked
function toggle(target) {
    var i,
    active_elements = document.getElementsByClassName("active"),
    clicked = document.getElementById(target['id']);

    if (!clicked.classList.contains("active")) {
        for (i = 0; i < active_elements.length; i++) {
            active_elements[i].classList.remove("active");
            console.log("removed");
        }
    }
    clicked.classList.toggle("active");
}
</script>

Related Posts:

  1. Order by meta value or date?
  2. Filtering posts by post meta data
  3. Custom query with query_posts doesn’t show post without certain meta_key
  4. How to sort by meta value?
  5. get_post_meta causes database queries
  6. Querying posts with meta value that begins with a certain pattern
  7. post_status => publish not working
  8. IF.. post meta show… Conditional Tag Help…?
  9. order posts by meta value on posts page
  10. Query_post($args)
  11. query_posts() on key’s value, or key’s existence
  12. How to retrieve an array of post IDs by a particular value stored in a custom meta’s array
  13. Query posts by meta_key whose value is an array
  14. Exclude posts by post meta value
  15. query_posts orderby postmeta [closed]
  16. wp_remote_post and form post
  17. Ordering Posts By Meta Data
  18. How to go to tag archives using a form
  19. get all posts with certain meta data
  20. Limiting the amount of posts this displays
  21. How to Get All Posts with any post status?
  22. posts_per_page no limit
  23. what is the correct way to compare dates in a WP query_posts meta_query
  24. How can i get count from query post
  25. query_post by title?
  26. Why query_posts() isn’t marked as deprecated?
  27. Alternative to query_posts for main loop? [duplicate]
  28. how to query posts by category and tag?
  29. Using WP_Query to Query Multiple Categories with Limited Posts Per Category?
  30. How do I query by post format in WordPress 3.1
  31. Display posts of the last 7 days
  32. Is there a way to exclude the content from the post variable to save on RAM usage?
  33. How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?
  34. Query posts by custom taxonomy ID
  35. Display/query post formats
  36. How to query post by user role?
  37. How to return results of a get_posts() in explicitly defined order
  38. Help to condense/optimize some working code
  39. How to set posts per page using WP_Query()
  40. query_posts exclude a meta key
  41. How do I create a random post that will last for a day
  42. Ensuring sticky posts are retrieved first (without using two queries)?
  43. Determine if more posts are available than was asked for in `query_posts()`?
  44. Modify main WordPress loop with a parse_query filter
  45. query_posts() in function makes global $wp_query out of sync?
  46. Limiting query_posts to 1, regardless of sticky post?
  47. How to make “sticky” pages (and query by them)
  48. Query posts: how to exclude results if post is in multiple categories
  49. Query Custom Meta Value with Increment
  50. query_posts ->using meta_compare / where meta value is smaller or greater or equals
  51. Problem with ‘post__not_in’
  52. How to order posts by descending comment count on taxonomy page?
  53. How to batch update post content with custom post meta value
  54. Sort posts alphabetically by custom field value, insert divider between different letters
  55. Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?
  56. WP 5.8 “Query Loop” block: where to place custom query?
  57. Sort X categories by last update and show image
  58. Custom Post Type “Event”: chronological list of recurring events
  59. using post__in allow duplicate post id
  60. Pagination on archive.php page
  61. WordPress Custom Query
  62. Best way to load page content in Fancybox popup?
  63. How should I intercept the main query and inject custom join / order by / group by criteria
  64. advice on creating a ‘related posts’ query like the one used on stackexchange
  65. order post my meta value m/d/y format with year as included value
  66. Order by meta value, pro first, then free
  67. Having trouble generating pagination links on custom query
  68. How to create an attachments archive with working pagination?
  69. _wp_page_template to dynamically use template
  70. Using dynamic conditions in ‘posts_where’ filter
  71. Help altering a query to exclude all but standard post format
  72. List users with the most total posts view
  73. Schedule Sticky Posts
  74. Use union/intersection query_posts variables in uri request parameter form?
  75. query_posts doesn’t order by title
  76. How to limit search to first letter of title?
  77. query_posts and pagination, still stuck after much research
  78. Query posts by taxonomy term name
  79. Sorting problem with ‘query_posts’ funcion in wordpress. Sort by custom field not working
  80. Total Count of Posts NOT in Selected Categories?
  81. how could I get the pagination as I want to when query posts using get_posts function
  82. Counter code for paginated category pages in wordpress
  83. Post Title displaying but not in the wrapped HTML I need
  84. Author List page: Exclude based on last post date
  85. update_post_meta for custom field not working upon form submission
  86. WordPress Search Filter Only for Page with Child of Child of Child of Child of Child
  87. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  88. What action hook updates post meta?
  89. Replace query_posts with pre_get_posts
  90. query_posts sort in multiple directions
  91. Filter query_posts by tag slug on “Tag Archive” page (when tag is 2 or more words)
  92. pagination in author.php template returns 404 error
  93. Save attachment custom fields on front end
  94. How to display liked posts of current user in wordpress?
  95. Problem with single-page for my custom post
  96. How to ensure that is_search() return false after query_posts
  97. Warning: urlencode() expects parameter 1 to be string, array given in and not getting the preffered output
  98. How to modify the query to exclude posts by slug?
  99. query recent posts from several categories
  100. Inserting Post Using wp_insert_post. How to Fill Yoast Plugin SEO Fields
Categories query-posts Tags forms, post-meta, query-posts, wp-update-post
How do I hide “Pages”, “Media”, “Comments”….for editor role?
How to validate my form

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress