esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; if( in_array( 'simplepress_forum', $_REQUEST['post_type'] ) ) { // Forums $table = SPFORUMS; // Try to find the forums if ( ! empty( $search ) ) { $forums = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE ( forum_name LIKE %s OR forum_name LIKE %s )", "%%{$search}%%", "{$search}%%" ) ); } else { $forums = $wpdb->get_results( "SELECT * FROM {$table}" ); } // Build the results array $results = array(); foreach ( $forums as $forum ) { // Results should meet same structure like posts $results[] = array( 'ID' => $forum->forum_id, 'post_title' => $forum->forum_name, ); } // Return our results wp_send_json_success( $results ); die; } else if( in_array( 'simplepress_topic', $_REQUEST['post_type'] ) ) { // Topics $table = SPTOPICS; // Try to find the topics if ( ! empty( $search ) ) { $topics = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE ( topic_name LIKE %s OR topic_name LIKE %s )", "%%{$search}%%", "{$search}%%" ) ); } else { $topics = $wpdb->get_results( "SELECT * FROM {$table}" ); } // Build the results array $results = array(); foreach ( $topics as $topic ) { // Results should meet same structure like posts $results[] = array( 'ID' => $topic->topic_id, 'post_title' => $topic->topic_name, 'post_type' => gamipress_simplepress_get_forum_title( $topic->forum_id ), ); } // Return our results wp_send_json_success( $results ); die; } } } add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_simplepress_ajax_get_posts', 5 ); // Helper function to get the forum title function gamipress_simplepress_get_forum_title( $forum_id ) { global $wpdb; $table = SPFORUMS; return $wpdb->get_var( $wpdb->prepare( "SELECT forum_name FROM {$table} WHERE forum_id = %d;", $forum_id ) ); } // Helper function to get the topic title function gamipress_simplepress_get_topic_title( $topic_id ) { global $wpdb; $table = SPTOPICS; return $wpdb->get_var( $wpdb->prepare( "SELECT topic_name FROM {$table} WHERE topic_id = %d;", $topic_id ) ); }