· 4 years ago · May 18, 2021, 06:04 AM
1<?php
2
3/**
4 * Callback function for custom REST API route
5 */
6
7function universitySearchResults( $data ) {
8 $mainQuery = new WP_Query( array(
9 'post_type' => array( 'post', 'page', 'professor', 'program', 'campus', 'event' ),
10 's' => sanitize_text_field( $data['term'] )
11 ) );
12
13 $results = array(
14 'generalInfo' => array(),
15 'professors' => array(),
16 'programs' => array(),
17 'events' => array(),
18 'campuses' => array()
19 );
20
21 while ( $mainQuery->have_posts() ) {
22 $mainQuery->the_post();
23
24 if ( get_post_type() == 'post' || get_post_type() == 'page' ) {
25 array_push( $results['generalInfo'], array(
26 'title' => get_the_title(),
27 'permalink' => get_the_permalink(),
28 'postType' => get_post_type(),
29 'authorName' => get_the_author()
30 ) );
31 }
32
33 if ( get_post_type() == 'professor' ) {
34 array_push( $results['professors'], array(
35 'title' => get_the_title(),
36 'permalink' => get_the_permalink(),
37 'image' => get_the_post_thumbnail_url( 0, 'professorLandscape' )
38 ) );
39 }
40
41 if ( get_post_type() == 'program' ) {
42 $relatedCampuses = get_field( 'related_campus' );
43
44 if ( $relatedCampuses ) {
45 foreach ( $relatedCampuses as $campus ) {
46 array_push( $results['campuses'], array(
47 'title' => get_the_title( $campus ),
48 'permalink' => get_the_permalink( $campus )
49 ) );
50 }
51 }
52
53 array_push( $results['programs'], array(
54 'title' => get_the_title(),
55 'permalink' => get_the_permalink(),
56 'id' => get_the_id()
57 ) );
58 }
59
60 if ( get_post_type() == 'campus' ) {
61 array_push( $results['campuses'], array(
62 'title' => get_the_title(),
63 'permalink' => get_the_permalink()
64 ) );
65 }
66
67 if ( get_post_type() == 'event' ) {
68 $eventDate = new DateTime( get_field( 'event_date' ) );
69
70 $description = null;
71 if ( has_excerpt() ) {
72 $description = get_the_excerpt();
73 } else {
74 $description = wp_trim_words( get_the_content(), 18 );
75 }
76
77 array_push( $results['events'], array(
78 'title' => get_the_title(),
79 'permalink' => get_the_permalink(),
80 'month' => $eventDate->format( 'M' ),
81 'day' => $eventDate->format( 'd' ),
82 'description' => $description
83 ) );
84 }
85 }
86
87 if ( $results['programs'] ) {
88 $programsMetaQuery = array( 'relation' => 'OR' );
89
90 foreach ( $results['programs'] as $item ) {
91 array_push( $programsMetaQuery, array(
92 'key' => 'related_programs',
93 'compare' => 'LIKE',
94 'value' => '"' . $item['id'] . '"'
95 ) );
96 }
97
98 $programRelationshipQuery = new WP_Query( array(
99 'post_type' => array( 'professor', 'event' ),
100 'meta_query' => $programsMetaQuery
101 ) );
102
103 while ( $programRelationshipQuery->have_posts() ) {
104 $programRelationshipQuery->the_post();
105
106 if ( get_post_type() == 'professor' ) {
107 array_push( $results['professors'], array(
108 'title' => get_the_title(),
109 'permalink' => get_the_permalink(),
110 'image' => get_the_post_thumbnail_url( 0, 'professorLandscape' )
111 ) );
112 }
113
114 if ( get_post_type() == 'event' ) {
115 $eventDate = new DateTime( get_field( 'event_date' ) );
116
117 $description = null;
118 if ( has_excerpt() ) {
119 $description = get_the_excerpt();
120 } else {
121 $description = wp_trim_words( get_the_content(), 18 );
122 }
123
124 array_push( $results['events'], array(
125 'title' => get_the_title(),
126 'permalink' => get_the_permalink(),
127 'month' => $eventDate->format( 'M' ),
128 'day' => $eventDate->format( 'd' ),
129 'description' => $description
130 ) );
131 }
132 }
133
134 $results['professors'] = array_values( array_unique( $results['professors'], SORT_REGULAR ) );
135
136 $results['events'] = array_values( array_unique( $results['events'], SORT_REGULAR ) );
137 }
138
139 return $results;
140}
141
142/**
143 * Registering custom REST API route - Search.js
144 */
145
146function universityRegisterSearch() {
147 register_rest_route( 'university/v1', 'search', array(
148 'methods' => WP_REST_SERVER::READABLE,
149 'callback' => 'universitySearchResults',
150 ) );
151}
152
153add_action( 'rest_api_init', 'universityRegisterSearch' );
154