Custom Fields
Support General Problems getting the post ID

  • Creator
  • #26512
    Resolved Martin


    I’ve been struggling getting any post IDs.

    So, my scenario should be simple. I have a MB form with a couple of buttons and a couple of text fields. I can get click actions on the buttons and send data to Ajax and get responses. The problem is that I can’t access post data in either the JQuery or the PHP function being called in the Ajax call.

    I’ve scoured lots of pages which give all sorts of ways to get the post IDs but none of them give me anything more than an empty string or in the case of one call I got the name of the button I’d clicked.

    This is my JQuery:

    jQuery( function ( $ ) {
        $( '#quote_sitter_availability' ).on( 'click', function() {
                type : "post",
                dataType : "json",
                url : myAjax.ajaxurl,
                data : {action: "quote_sitter_availability", post_id: myAjax.post_id},
                success: function(response) {
                    if(response.type == "success") {
                       $( '#quote-actions' ).find( '#quote_sitter_availability_done' ).val(response.title);
                    else {
                        $( '#quote-actions' ).find( '#quote_sitter_availability_done' ).val(response.title);
                        alert("That didn't work");
            } );
        } );
    } );

    And this is my PHP (added using Code Snippets):

    add_action( 'init', 's4p_enqueue_custom_script' );
    function s4p_enqueue_custom_script() {
        wp_register_script( 's4p_script', get_template_directory_uri() . '/assets/js/admin.js', array( 'jquery' ), '', true );
        $vars_array = array( 'ajaxurl' => admin_url( 'admin-ajax.php'), 'post_id' => 2747 );
        wp_localize_script( 's4p_script', 'myAjax', $vars_array);
        wp_enqueue_script( 's4p_script' );
    add_action("wp_ajax_quote_sitter_availability", "quote_sitter_availability");
    function quote_sitter_availability() {
        $returned_title .= " - " . get_post_meta($_REQUEST["jobid"], "job_status", true) . " - ";
        $returned_title .= "test";
        $result['type'] = "success";
        $result['title'] = $returned_title;
        $result = json_encode($result);
        echo $result;

    The form is one of several I have attached to my CPT of Job accessed only in the Admin Dashboard.

    All I need is for the ID to be available in the PHP code (whether it’s passed in the JQuery or picked up within the PHP function doesn’t matter to me) so I can read and write meta data etc.


Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.