Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action('rest_api_init', 'universityRegisterSearch');
- function universityRegisterSearch(){
- register_rest_route('university/v1', 'search', [
- 'methods' => WP_REST_Server::READABLE, // Wordpress alias for the GET method (we could write also GET)
- 'callback' => 'universitySearchResults', // callback name of a function that we will use
- ]);
- }
- // The callback function
- function universitySearchResults($data) {
- $mainQuery = new WP_Query(array(
- 'posts_per_page' => -1,
- 'post_type' => array('post', 'page', 'professor', 'program', 'campus', 'event'),
- 's' => sanitize_text_field($data['term'])
- ));
- $results = array(
- 'generalInfo' => array(),
- 'professors' => array(),
- 'programs' => array(),
- 'events' => array(),
- 'campuses' => array()
- );
- while($mainQuery->have_posts()) {
- $mainQuery->the_post();
- if (get_post_type() == 'post' OR get_post_type() == 'page') {
- array_push($results['generalInfo'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink(),
- 'postType' => get_post_type(),
- 'authorName' => get_the_author()
- ));
- }
- if (get_post_type() == 'professor') {
- array_push($results['professors'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink(),
- 'image' => get_the_post_thumbnail_url(0, 'professorLandscape'), // 0 means the current post
- ));
- }
- if (get_post_type() == 'program') {
- $relatedCampuses = get_field('related_campus');
- if ($relatedCampuses) {
- foreach ($relatedCampuses as $campus) {
- array_push($results['campuses'], [
- 'title' => get_the_title($campus),
- 'permalink' => get_the_permalink($campus),
- ]);
- }
- }
- array_push($results['programs'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink(),
- 'id' => get_the_ID(),
- ));
- }
- if (get_post_type() == 'campus') {
- array_push($results['campuses'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink()
- ));
- }
- if (get_post_type() == 'event') {
- $eventDate = new DateTime(get_field('event_date'));
- $month = $eventDate->format('M');
- $day = $eventDate->format('d');
- array_push($results['events'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink(),
- 'month' => $month,
- 'day' => $day,
- 'excerpt' => has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 18),
- ));
- }
- }
- if ($results['programs']){
- $programsMetaQuery = [
- 'relation' =>'OR',
- ];
- foreach($results['programs'] as $item){
- $programsMetaQuery[]= [
- 'key' => 'related_programs',
- 'compare'=> 'LIKE',
- 'value' => '"' . $item['id']. '"',
- ];
- }
- $progamRelationshipQuery = new WP_Query([
- 'posts_per_page' => -1,
- 'post_type' => [
- 'professor',
- 'event',
- ],
- 'meta_query' => $programsMetaQuery,
- ]);
- while($progamRelationshipQuery->have_posts()){
- $progamRelationshipQuery->the_post();
- if (get_post_type() == 'event') {
- $eventDate = new DateTime(get_field('event_date'));
- $month = $eventDate->format('M');
- $day = $eventDate->format('d');
- array_push($results['events'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink(),
- 'month' => $month,
- 'day' => $day,
- 'excerpt' => has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 18),
- ));
- }
- if (get_post_type() == 'professor') {
- array_push($results['professors'], array(
- 'title' => get_the_title(),
- 'permalink' => get_the_permalink(),
- 'image' => get_the_post_thumbnail_url(0, 'professorLandscape'), // 0 means the current post
- ));
- }
- }
- // array_values removes the numeric keys from associative array that creates the array_unique
- // array_unique removes the duplicate elements from the array and the SORT_REGULAR creates an associative array
- $results['professors'] = array_values(array_unique($results['professors'], SORT_REGULAR));
- $results['events'] = array_values(array_unique($results['events'], SORT_REGULAR));
- }
- return $results;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement