1

I am having this function in my functions.php

function get_blueprints_for_building () {

$args = array( 'post_type' => 'portfolio');

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    echo get_the_ID();
endwhile;
  exit();
}

then this function is hooked to be able to be called by AJAX as the following:

add_action('wp_ajax_blueprints_for_building', 'get_blueprints_for_building');
add_action('wp_ajax_nopriv_blueprints_for_building', 'get_blueprints_for_building');

The problem that I am having is whenever I am on a specific page, and this function is being executed, I need the ID of the current page, but the function returns all the pages that matches the post_type => portfolio not just that specific one I am currently at.

The question is that how can i get back that only one id using the WP_Query?

UPDATE - What am I trying to achieve here?

I am on a page, lets call it portfolio-1 as an example.

As soon as the portfolio-1 page is loaded, I am executing an AJAX, which calls the php function mentioned above.

I would like to get the ID of that current page (the portfolio-1) because on an other table in the database there are stuff related to this page, connected by the page id. So far, that database query is not in the function, as I was just trying to achieve to get the id of the current page. If the id is provided, i will just call an other function with this id as a parameter, like:

get_the_other_stuff($currentPageId)

But that's not what the question is about right now. :)

6
  • How does a function that you call via AJAX know, which page you called it from?
    – kero
    Jul 4, 2017 at 14:14
  • @kero well that's a good point. i thought the query handles that, but if not, how can i tell it? Jul 4, 2017 at 14:17
  • That depends how you are creating the ajax request. From what you wrote it sounds like you want to make a request from a page to the backend only to get the name of the page you are currently on - this doesn't really make sense to me. Can you elaborate on what you're trying to achieve (in the question itself please!)?
    – kero
    Jul 4, 2017 at 14:23
  • @kero updated with explanation! Jul 4, 2017 at 14:28
  • 1
    What you are trying to do is not okay. What if the AJAX is called from somewhere outside the loop? You should pass the ID to the callback function manually.
    – Johansson
    Jul 4, 2017 at 14:29

1 Answer 1

1

Since the AJAX can be called from anywhere, even by a search engine, you should pass the ID manually. To do this, you need to include the ID of your current page somewhere in your template.

There is a common practice to include extra information in hidden input elements. You can add this hidden element to your template file:

<input id="my-post-id" type="hidden" value="<?php echo get_the_ID();?>">

Now, you can get the post's ID in your AJAX call:

var theID;
theID = jQuery("#my-post-id").val();

This way, you can include this value in your call:

function customFunction() {
    var theID;
    theID = jQuery("#my-post-id").val();
    jQuery.ajax({
        type: 'GET', 
        url: 'AJAX URL HERE', 
        data: { postid: theID }, 
        // The rest of the AJAX here
    }
};

Now, you can check if the ID is set in your Admin-AJAX:

function get_blueprints_for_building () {
    // Stop execution if the function is called from out of the page
    if (!isset($_GET['postid'])) exit('Please set a post ID!');
    $id = $_GET['postid'];
    // Now we have the ID!
}

There you go.

Note!

I would also recommend that you use the REST API instead of Admin-AJAX. It's easier and faster to set up. Take a look at my answer here.

UPDATE

Instead of creating a hidden input, you can also use wp_localize_script to pass your ID to the script. You need to have an enqueued script though.

wp_localize_script( 
    'my-js', // The ID of your enqueued JS file
    'my_localized_js', // The prefix for object
    $my_localized_array // The array that contains your data
);

Now, you can set the ID of the current page in your array:

$my_localized_array = array(
    'postID' => get_the_ID,
);

After doing this, you can access your ID in the JS file by using this:

var id = my_localized_js.postID;

Which can be used in the AJAX call later.

2
  • 1
    I suggest wp_localize_script() to pass variables to the ajax script. Using an HTML element just to pass a variable to js is not very good approach, althoug it can work. See this for a quick example.
    – cybmeta
    Jul 4, 2017 at 15:14
  • 1
    @cybmeta Thank you I forgot about that. I updated the answer. The name localization is misleading, I always forget about it.
    – Johansson
    Jul 4, 2017 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.