I'm creating some dynamic content but I need to run a conditional is_single() check inside the php function

add_action( 'wp_ajax_rando_func', 'rando_func' );
add_action( 'wp_ajax_nopriv_rando_func', 'rando_func' );

function rando_func() {
   if (is_single()) {
        //do stuff

This doesn't work so I tried adding the ID

function rando_func() {
   $page_id = $_POST["page_id"];
   if (is_single($page_id)) {
        //do stuff

No luck with that either. How do I accomplish this?

  • It needs to be $_POST["page_id"], do you have the same error in your code?
    – kero
    Commented Sep 7, 2017 at 12:40
  • Sorry that was just a typo
    – user300979
    Commented Sep 7, 2017 at 12:41
  • Still an error id"} -> id"]. Then it should work. Are you sure, $_POST['id'] has the id, because then it should work. Can you show, how you are calling the function from your frontend?
    – kero
    Commented Sep 7, 2017 at 12:44
  • Yes I added it as data to the parent container to test that it's passing through.
    – user300979
    Commented Sep 7, 2017 at 12:49
  • To my mind knowing which template is rendered in the AJAX response isn't the way to go. I'd suggest having different AJAX actions based on what you want to receive. So on a single post you'd send a different request that you would if it were an archive. Then you output the right action in the template or with wp_localize_script() based on is_single() etc. Commented Sep 7, 2017 at 13:51

1 Answer 1


is_single returns if there is a single page displayed within the actual server process. Within the Ajax-Function, nothing is "displayed", you're doing Ajax.

To check if the currently displayed object in your Browser is a single object, you need to transfer the information about this from the server process that serves your Browser to the server process that handles your ajax request.

Please try the following: Step 1: get the information into your Javascript function. You get the information from the $wp_query object. If you directly echo the javascript which does the AJAX Request, you can do it like this:

var is_single = <?php echo (is_single() ? '1' : '0' ); ?>;

If you're enqueueing your ajax-caller-script, you can use wp_localize_script .

Step 2: POST the is_single information to the Ajax Function

Step 3: check edit below! Change your Ajax PHP Function to incorporate the is_single information:

function rando_stuff(){

 $single = (int)$_POST['is_single'];
       //do stuff

Step 4: Profit ;)

============================================ EDIT 2022-02-20:

5 Years ago, this was how i coded. However, since then i learned a bit more about proper working with POST values, so i rewrote the Function for step 3. For Explanation: Values transmitted per POST or GET don't have data types, so they are interpreted as strings. To use these Values as Boolean (true/false) data types, we have to typecast them. Luckily, Wordpress has a function to properly interpret this.

function rando_stuff_improved(){
     $is_single = false;
     if( isset( $_POST['is_single'] ) ){
        $is_single = rest_sanitize_boolean( $_POST['is_single'] );
     if( $is_single ){
        //do stuff
  • Shouldn't is_single( id ) return, if id is a single? No need for current displayed, loop, etc
    – kero
    Commented Sep 7, 2017 at 13:04
  • @kero An ID can't be is_single because is_single is a property of the main query, not of a page or post. Commented Sep 7, 2017 at 13:25
  • Yes and no. The important thing to understand is that with ajax functions, you have two seperate processes that the server does: Process 1 is the one that displays your website in your browser. Within this process, is_single may be true (because maybe your browser shows the single post or page) Process 2 is the one where your ajax request is processed. In this process, is_single is always false, because nothing is displayed. You don't request a single post from the browser, instead the javascript acts as a quasi-browser to request the admin-ajax.php. Commented Sep 7, 2017 at 13:26
  • A somewhat example to explain: You (the ajax request) and your twin brother (the "normal" browser) both go and buy cars at two different places. The merchant you go to says: i'll give you an offer for 200$ less, but only if you can proof that your brother bought a green car (do stuff if is_single()). If you don't have a picture of your brothers new green car (The POST data you have to transmit with the information is_single), the salesman will never give you that offer. Commented Sep 7, 2017 at 13:31
  • I come a bit late on this thread but the solution provided by @HUistSebastian works for me. My only question is why using (int) in front of $_POST['is_single'];? Thanks. Commented Feb 19, 2022 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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