How to check category name available or not

Question

I have an input field and when users enter any text then that will check the category name is available or not which user entered.

Js

(function($) {  // ready handler
$(".universalSearchField").keypress(function() {
     $.ajax({
        url: "/wp-admin/admin-ajax.php",
        type: "post",
        data: { action: "universalSearchlist", keyword: $("#searchdata").val() },
        success: function(data) {
            console.log(data);
           // $("#datafetch").html( data );
        }
    });
    });

    })(jQuery);

Function.php

 add_action('wp_ajax_universalSearchlist','universalSearch');
 add_action('wp_ajax_nopriv_universalSearchlist','universalSearch');
function universalSearch(){
$the_query = new WP_Query( array('category_name' => esc_attr( $_POST['universalSearchlist'] ), 'post_type' => 'post' ) );
    foreach ( $the_query->posts as $p ) {
    $categories = get_the_category($p->ID);
    if (!empty($categories) ) {
        echo esc_html( $categories[0]->name );   
    }
}
    
}

I am getting all the categories and I want when the user starts to enter the text box then start checking the category name is available or not.

0
, , Naren Verma 3 years 2020-08-26T08:10:38-05:00 0 Answers 43 views 0

Leave an answer

Browse
Browse