admin管理员组

文章数量:1125307

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

function.php

  <?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 ) {
           $certain_post='post_b';
        } elseif ( 'post' == $post->post_type ) {
             $certain_post='post';
        }
    }
    
    
    $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');
        endwhile;
        wp_reset_postdata();
    endif;

    echo json_encode($response);
    die();
}

本文标签: Get current post type within functionsphp