In my function.php I need to run something to check post type or page id.

how can i check current post type loaded in functions.php


// the ajax function
add_action('wp_ajax_filterPosts', 'filterPosts');
add_action('wp_ajax_nopriv_filterPosts', 'filterPosts');

function filterPosts()
     global $post;

    if( is_singular() ) {
        if( 'post_type_b' == $post->post_type ) {
        } elseif ( 'post' == $post->post_type ) {
    $response = [
        'posts' => "",
     // New args for an All Posts Query
    $all_args = array(
        'posts_per_page' => -1, // returns all posts
        'post_status' => 'publish',
        'post_type' => $certain_post

    $filter_args = array(
        'post_status' => 'publish',
        'post_type' => $certain_post

    if ($_POST['limit']) {
        $filter_args['posts_per_page'] = $_POST['limit'];

    if ($_POST['category']) {
        $filter_args['cat'] = $_POST['category'];
         // Get the total number of posts for the category
        $all_args['cat'] = $_POST['category'];

    $filter_query = new WP_Query($filter_args);
    // New All Posts Query
    $all_query = new WP_Query($all_args);

    if ($filter_query->have_posts()) :
        while ($filter_query->have_posts()) : $filter_query->the_post();
            $response['posts'] .= load_template_part('/template-parts/posts-filter/single-post');

    echo json_encode($response);
  • 1
    An AJAX handler doesn't know which page requested it so functions like is_singular or using global variables don't make sense, the only way it can know is if you pass it from javascript in the request and read it in a GET variable. The same for anything else about the page that made the AJAX request. Remember, each request to the server starts with a fresh blank slate, no variables survive once the request is finished. If you want things to carry over you got to save them somewhere, DB/Cookie/files/etc/etc, there's no persistent long running web program like in NodeJS/Java
    – Tom J Nowell
    Feb 9 at 19:17
  • @TomJNowell dam, seems im screwed. been following this tutorial but now want to make the filtered post depending on what post im on. joeyfarruggio.com/wordpress/posts-category-filter/…
    – Beep
    Feb 9 at 19:38
  • there anyway to get something i can write a statement from, even current url ?
    – Beep
    Feb 9 at 20:27
  • the article you've linked to doesn't contain any of the mistakes I mentioned, an AJAX handler doesn't know who or what has called it, it only knows what it has been given. If it needs to know if the page calling it is is_singular() === true then it can't find that out on its own, just like you followed the tutorial and grabbed the category and sent it to the AJAX handler via POST if you want it to know which page its on or the post type then it has to be told and passed that information hint hint
    – Tom J Nowell
    Feb 12 at 15:45
  • likewise it can't find out the URL on its own, that can't be done from the PHP side but it can be done on the javascript side and passed along
    – Tom J Nowell
    Feb 12 at 15:46


Your Answer

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

Browse other questions tagged or ask your own question.