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

Update status, meta while inside a post using AJAX button

First things first

In a wp_ajax_{action} or wp_ajax_nopriv_{action} callback, the global $post variable is by default a null because WordPress has not yet set it up. You can confirm it using a simple callback like so:

add_action( 'wp_ajax_test', function(){
    global $post;
    var_dump( $post );
    wp_die();
} );

Visit /wp-admin/admin-ajax.php?action=test and you should see NULL displayed on the page. If you don’t see a NULL, then it’s possible that a plugin or custom code has made a query which changed the $post variable.

Now a solution

Since you’re making the AJAX request via a button click and the button is inside a meta box which I suppose is on the post edit screen (wp-admin/edit.php), then an easy way to fix the issue of the $post being still a NULL in your wp_ajax_change_status callback, is to send the post ID from your AJAX script (JavaScript).

On the post edit screen, the post ID is stored in a hidden input field named post_ID which looks like this and stores the ID of the post that’s currently being edited:

<input type="hidden" id='post_ID' name="post_ID" value="123" />

So you can use the value and send it to your AJAX callback. E.g.:

jQuery.ajax({
    url: '/wp-admin/admin-ajax.php',
    data: {
        action:  'change_status',
        post_id: jQuery( '#post_ID' ).val() // send the post ID
    },
    type: 'post'
});

Then in your AJAX callback (i.e. the ew_change_status() function), you can retrieve the submitted post ID like so:

$post_id = filter_input( INPUT_POST, 'post_id' );

Additional Notes

  1. In your AJAX callback, make sure to check whether the current user can edit the post. E.g.:

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        wp_die();
    }
    
  2. Use a nonce (because you are editing a post). For example,

    a) In your meta box, create a hidden input for storing the nonce:

    // Creates an <input> with the `id` and `name` "my_security".
    <?php wp_nonce_field( 'change-post-status', 'my_security' ); ?>
    

    b) Include the nonce in the AJAX request:

    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',
        data: {
            action:  'change_status',
            post_id:  jQuery( '#post_ID' ).val(),
            security: jQuery( '#my_security' ).val()
        },
        type: 'post'
    });
    

    c) In your AJAX callback, verify the nonce:

    check_ajax_referer( 'change-post-status', 'security' );
    

So your ew_change_status() may look like:

function ew_change_status() {
    check_ajax_referer( 'change-post-status', 'security' );

    $post_id = filter_input( INPUT_POST, 'post_id' );
    if ( ! $post_id || ! ( $post = get_post( $post_id ) ) ) {
        wp_die( 'No such post, or invalid ID.' );
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        wp_die( 'You can not edit this post.' );
    }

    ... your code here ...

    wp_die();
}

Related Posts:

  1. Long Polling: Stuck in while loop
  2. How to update Comment post meta through an Ajax call
  3. Update attachment metadata fails
  4. Attach time/date stamp on add_post_meta
  5. wp_update_post onclick button using ajax
  6. bulk update meta value with ajax
  7. Filter images from media library by guid meta field
  8. Want to send ajax request in wordpress to a custom file in plugin
  9. delete post meta data in array WordPress
  10. Allow guest to update custom post met using ajax
  11. Data from ajax not updating post meta
  12. delete attachment for one post without deleting actual attachment post
  13. Ajax takes 10x as long as it should/could
  14. How to call a PHP function with Ajax when the user clicks a button
  15. Ajax for non-logged-in users
  16. Is there any way of of making an admin-ajax request without the use of die()?
  17. Ajax request returning full page code
  18. How-to debug wp_ajax_* hook callback?
  19. How to enable users to down-vote in this simple voting counter (that uses the post meta)?
  20. AJAX Implementation
  21. how to use ajax in plugin admin area?
  22. edit-comments.php comment_row_actions ajax problem
  23. jQuery UI Autocomplete showing all results
  24. What action hook updates post meta?
  25. Why is my AJAX call not working?
  26. How to Access custom database content with AJAX onClick refresh of div inside member-only WordPress page?
  27. how to use reCaptcha v3 in wordpress custom login form?
  28. admin-ajax.php HTTP400: BAD REQUEST – The request could not be processed by the server due to invalid syntax
  29. WP_Session not acting with AJAX
  30. Ajaxing in functions.php
  31. Embedded Twitter feed won’t render nicely when loaded via Ajax
  32. Enqueue script in header
  33. Using ajax with wordpress
  34. Using foreach inside an ajax function
  35. WP_User_Query ignoring ‘meta_query’ arguments
  36. Ajax image upload with media_handle_upload and form.js
  37. Edit a different page in WP Customizer
  38. Get the_content with ajax
  39. Caching-Plugins and Ajax-Page-Parts
  40. Ajax loading duplicate post
  41. Admin ajax error 400 when passing select value to populate another select
  42. How do I detect in which page ajax_query_attachments_args is loaded?
  43. Ajax post returning full html page as response
  44. Which allowed API hooks work to add wp_ajax action?
  45. AJAX request randomly stop working and returns error 400
  46. Sending variable from ajax on form submit
  47. Ajax store response json in variables
  48. How can I set cookies on both secure and non-secure origins at the same time?
  49. Variable Products Being Added to Cart with AJAX on Shop and Category Pages
  50. Save & Reset button in theme-option with Ajax (without refresh)
  51. Ajax Multi Response Problem
  52. AJAX call inside plugin class not triggering
  53. Force redirect single.php to index
  54. Any plugin or Script to integrate ajax selection module into WordPress page
  55. Custom AJAX Endpoint not returning any result
  56. Contact form 7 Hide response messages after 5 seconds
  57. how should i get json encoded data from wordpress ajax action page
  58. Cache plugins and ajax nonce verification
  59. Ajax function on #publish only saves as draft – how to make it publish?
  60. How to include php-generated javascript?
  61. Serve a different theme based on window size
  62. custom autocomplete search
  63. Issues Updating Post Meta with AJAX (Seems simple but cannot figure it out)
  64. ajax problem – function on server is not called
  65. Is it possible to determine whether a page is a page template by page_id in ajax call?
  66. 200 return code on ‘POST /wp-admin/admin-ajax.php’ while NOT logged in
  67. i’m trying to get all my media query attachments via ajax in wordpress
  68. Distinguish between 2 instances of admin-ajax.php
  69. How to update post with Ajax (no plugin)
  70. Next Ajax call doesn’t work
  71. Ajax page load without reload
  72. need a confirmation text to appear on email submission
  73. Why does reCAPTCHA v3 return 0 ? In custom AJAX registration form
  74. admin-ajax.php 400 bad request
  75. WordPress Ajax Page Load to skip embedded iframe
  76. edit user input data contact form 7
  77. Set cors header for ajax requests
  78. Simple AJAX code that refreshes every x seconds?
  79. Search function – problem with whole words
  80. jQuery ajax method does not return data
  81. Ajax is not defined
  82. Get posts by category via ajax
  83. Download doccument on server rather than clients browser
  84. Load oEmbed iframe within ajax call
  85. WordPress 4.9.6 – IncludeMe & getAjax GET using wrong URL
  86. Create secondary Archive page format?
  87. Get post details with pure javascript ajax
  88. WorddPress website admin part not working correctly – I think ajax/json issue
  89. Ajax Security regarding user priviliges and nonces
  90. Use ajax without a plugin?
  91. Weird admin-ajax.php problem
  92. Ajax contact form returnig 0
  93. Know which script/page is being called by ajax call
  94. Can’t update WP Editor after Ajax
  95. Posting to loop.php file
  96. Running js in html code with same content
  97. How to get current_user_id from wordpress in node js?
  98. how can i create get request unis ajax without expecting return value
  99. AJAX call not initializing for non-admins in WordPress
  100. Help with jquery/ajax requests
Categories ajax Tags ajax, post-meta, wp-update-post
Creating two tables in database on activation hook
I cannot login and am getting this error message. .

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