• What best practice to handle user location in website when ajax request via admin-ajax.php processed, (single, category, posts etc)?

    Because, now its not working for me.

    <?
    
    add_action( 'wp_ajax_swp_global_request', 'swp_global_request' );
    add_action( 'wp_ajax_nopriv_swp_global_request', 'swp_global_request' );
    
    function swp_global_request(){
    
    if (is_single()){
     // not working
    }
    
    } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi misterixas111,

    Requests to admin-ajax.php don’t have a “location” in the same way that theme template files do. None of the is_<condition>() functions related to WP_Query will give you anything except false in this setting.

    What exactly are you trying to accomplish?

    i had a similar issue, i loaded posts via admin-ajax but their comments and the $more content was missing.
    i echoed out is_single() and it was false, when loaded via ajax.
    so what you could do inside your template, is set the variable to true yourself like so:

    global $wp_query;
    $wp_query->is_single = true;

    i guess in your case you have to submit a variable inside your request, that defines what condition you want to archive..

    • This reply was modified 7 years, 7 months ago by jnz31.

    Hi @jnz31,

    You should never directly modify the WordPress global variables. Changing the $wp_query variable in particular can have unexpected consequences elsewhere in your code.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘is_single in admin-ajax.php request’ is closed to new replies.