'invalid_nonce', 'message' => __( 'Your session has expired. Please reload the page.', 'admore-widgets-suite' ) ], 403 ); } $query = sanitize_text_field( wp_unslash( $_POST['query'] ?? '' ) ); $post_types = array_map( 'sanitize_key', (array) ( $_POST['post_types'] ?? [ 'post', 'page' ] ) ); $max_result = min( 20, absint( $_POST['max_results'] ?? 6 ) ); if ( mb_strlen( $query ) < 2 ) { wp_send_json_success( [ 'results' => [] ] ); } $allowed_types = get_post_types( [ 'public' => true ], 'names' ); $post_types = array_values( array_intersect( $post_types, $allowed_types ) ); if ( empty( $post_types ) ) { $post_types = [ 'post', 'page' ]; } $args = [ 'post_type' => $post_types, 'post_status' => 'publish', 'posts_per_page' => $max_result, 's' => $query, 'no_found_rows' => true, ]; // Category filter: taxonomy + term_id $taxonomy = sanitize_key( wp_unslash( $_POST['taxonomy'] ?? '' ) ); $term_id = absint( $_POST['term_id'] ?? 0 ); if ( $taxonomy && $term_id && taxonomy_exists( $taxonomy ) ) { $args['tax_query'] = [ [ 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $term_id, ], ]; } $search = new WP_Query( $args ); $results = []; if ( $search->have_posts() ) { foreach ( $search->posts as $post ) { $thumb_url = ''; if ( has_post_thumbnail( $post->ID ) ) { $thumb_url = get_the_post_thumbnail_url( $post->ID, 'thumbnail' ); } $excerpt = ''; if ( post_password_required( $post ) ) { // Never reveal any part of a password-protected post. $excerpt = ''; } elseif ( ! empty( $post->post_excerpt ) ) { $excerpt = wp_trim_words( $post->post_excerpt, 12, '…' ); } elseif ( ! empty( $post->post_content ) ) { $excerpt = wp_trim_words( wp_strip_all_tags( $post->post_content ), 12, '…' ); } $pt_obj = get_post_type_object( $post->post_type ); $results[] = [ 'id' => $post->ID, 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'type' => $pt_obj ? $pt_obj->labels->singular_name : $post->post_type, 'thumbnail' => $thumb_url, 'excerpt' => $excerpt, ]; } } wp_reset_postdata(); wp_send_json_success( [ 'results' => $results ] ); } }