Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'wp_ajax_jeg_find_post', 'jeg_find_post' );
- function jeg_find_post() {
- if ( isset( $_REQUEST['nonce'], $_REQUEST['query'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'jeg_find_post' ) ) {
- $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) );
- if ( (bool) $query ) {
- add_filter(
- 'posts_where',
- function ( $where ) use ( $query ) {
- global $wpdb;
- // if ( isset( $_REQUEST['string'] ) && ! empty( $_REQUEST['string'] ) ) {
- // $string = $_REQUEST['string'];
- $where .= $wpdb->prepare( "AND {$wpdb->posts}.post_title LIKE %s", '%' . $wpdb->esc_like( $query ) . '%' );
- // }
- return $where;
- }
- );
- }
- $query = new \WP_Query(
- [
- 'post_type' => 'post',
- 'posts_per_page' => '15',
- 'post_status' => 'publish',
- 'orderby' => 'date',
- 'order' => 'DESC',
- ]
- );
- $result = [];
- if ( $query->have_posts() ) {
- while ( $query->have_posts() ) {
- $query->the_post();
- $result[] = [
- 'value' => get_the_ID(),
- 'text' => get_the_title(),
- ];
- }
- }
- wp_reset_postdata();
- wp_send_json_success( $result );
- }
- }
Add Comment
Please, Sign In to add comment