-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
254 lines (196 loc) · 8.23 KB
/
search.php
File metadata and controls
254 lines (196 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* The template for displaying search results pages
*/
get_header();
global $wp_query;
if( have_posts() ):
$pagination_args = array(
'prev_text' => '<span class="ccl-h3 ccl-u-mr-1">‹ ' . __( 'Previous page', 'ccl' ) . '</span>',
'next_text' => '<span class="ccl-h3 ccl-u-ml-1">' . __( 'Next page', 'ccl' ) . ' ›</span>',
'before_page_number' => '<span class="ccl-h3" style="margin-left:0.5rem;margin-right:0.5rem"><span class="meta-nav screen-reader-text">' . __( 'Page', 'ccl' ) . ' </span>',
'after_page_number' => '</span>'
);
//create an empty array for filtered results to be sorted,
//each primary array key will be a post type, ie guide, page, staff, etc
$sorted_search_results = array(
'guide' => array(
'title' => '<span class="ccl-b-icon compass" aria-hidden="true"></span> Guides',
'slug' => 'Curated list of the best research tools and resources for a discipline or subject',
),
'faq' => array(
'title' => '<span class="ccl-b-icon question" aria-hidden="true"></span> FAQs',
'slug' => 'Quick answers to frequently asked questions about the library and our services',
),
'webpage' => array(
'title' => '<span class="ccl-b-icon clip" aria-hidden="true"></span> Library Website',
'slug' => 'Content from the library website',
),
'staff' => array(
'title' => '<span class="ccl-b-icon person" aria-hidden="true"></span> Staff',
'slug' => 'Library staff',
),
'news' => array(
'title' => '<span class="ccl-b-icon calendar" aria-hidden="true"></span> News',
'slug' => 'Information about new things happening at the library',
),
'database' => array(
'title' => '<span class="ccl-b-icon pointer-right" aria-hidden="true"></span> Databases',
'slug' => 'Electronic resources available through the library',
),
'general' => array(
'title' => '<span class="ccl-b-icon clip" aria-hidden="true"></span> Resources',
'slug' => 'General resources offered by the library',
)
);
$temp_guide_posts = array();
$temp_webpage_posts = array();
$temp_staff_posts = array();
$temp_database_posts = array();
$temp_faq_posts = array();
$temp_general_posts = array();
$temp_news_posts = array();
//start sorting through each post, pull each out, and assign correct custom data
foreach( $wp_query->posts as $post ){
//identify each post type and assign the appropriate data
switch($post->post_type){
case 'guide':
$temp_guide_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_post_meta( $post->ID, 'guide_friendly_url', true ) ?: site_url('/research-guides/'),
'slug' => $post->post_name,
);
break;
case 'page':
$temp_webpage_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_permalink( $post->ID ) ?: $post->guid,
'slug' => $post->post_name,
);
break;
case 'staff':
$temp_staff_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_post_meta( $post->ID, 'member_friendly_url', true ) ?: site_url('/staff-directory/'),
'slug' => $post->post_name,
);
break;
case 'database':
$temp_database_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_post_meta( $post->ID, 'database_friendly_url', true ) ?: site_url('/database-directory/'),
'slug' => $post->post_name,
);
break;
case 'faq':
$temp_faq_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_post_meta( $post->ID, 'faq_friendly_url', true ) ?: 'https://claremont.libanswers.com/',
'slug' => $post->post_name,
'content' => $post->post_content
);
break;
case 'news':
$temp_news_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_permalink( $post->ID ) ?: get_post_type_archive_link( 'news' ) ,
'slug' => $post->post_name,
);
break;
default:
$temp_general_posts[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_url' => get_post_meta( $post->ID , 'general_url', true ) ?: site_url() ,
'slug' => $post->post_name,
);
}
}
//assign to the appropriate array
$sorted_search_results['guide']['posts'] = $temp_guide_posts;
$sorted_search_results['webpage']['posts'] = $temp_webpage_posts;
$sorted_search_results['staff']['posts'] = $temp_staff_posts;
$sorted_search_results['database']['posts'] = $temp_database_posts;
$sorted_search_results['faq']['posts'] = $temp_faq_posts;
$sorted_search_results['news']['posts'] = $temp_news_posts;
$sorted_search_results['general']['posts'] = $temp_general_posts;
//check to make sure there are posts for each post type
$sorted_search_results = array_filter( $sorted_search_results, function( $k ){
return !empty( $k['posts'] );
} );
endif;
?>
<div id="content" class="site-content">
<header class="ccl-c-hero">
<div class="ccl-l-container">
<div class="ccl-l-row">
<?php if ( have_posts() ): ?>
<div class="ccl-l-column ccl-l-span-two-thirds-lg">
<h1> <?php printf( esc_html__( 'Search Results for : %s', '_s' ), '<strong><br />' . get_search_query() . '</strong>' ); ?> </h1>
<div class=" ccl-u-ml-2 ccl-u-mt-1 ccl-u-weight-bold "><?php echo $wp_query->found_posts; ?> results found</div>
</div>
<div class="ccl-l-column ccl-l-span-third-lg">
<div class="ccl-c-hero__content">
<ul class="ccl-c-hero__menu">
<?php foreach ( $sorted_search_results as $key => $result ) : ?>
<?php $result_count = count( $result['posts'] ); ?>
<li><a class="js-smooth-scroll" href="#<?php echo $key; ?>"><?php echo $result['title'] . ' ('. $result_count .')' ; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php else: ?>
<div class="ccl-l-column ccl-l-span-two-thirds-lg">
<h1><?php printf( esc_html__( 'No Results for : %s', '_s' ), '<strong>' . get_search_query() . '</strong>' ); ?> </h1>
</div>
<?php endif; ?>
</div>
</div>
</header>
<div class="ccl-l-container">
<div class="ccl-u-mt-2">
<?php
if( have_posts() ) :
$post_type_count = 0;
//check for items in post type, then render
foreach( $sorted_search_results as $key => $result ): ?>
<div id="<?php echo $key; ?>" class="ccl-l-row ccl-u-mt-2 ccl-c-search-page <?php //echo ( $post_type_count % 2 === 0 ) ? '' : 'ccl-l-offset-2-lg'; ?>">
<div class="ccl-l-column ccl-l-span-12 ccl-l-span-6-md ccl-l-span-4-lg ccl-c-search-page__header">
<h3 class="ccl-c-search-page__title"><?php echo $result['title']; ?></h3>
<div class="ccl-u-faded ccl-c-search-page__slug">
<?php echo $result['slug']; ?>
</div>
</div>
<div class=" ccl-c-column ccl-c-search-page__content">
<ul class="ccl-c-search-page__items ccl-u-clean-list">
<?php foreach( $result['posts'] as $key => $post): ?>
<li class="ccl-c-search-page__list-links"><a href="<?php echo $post['post_url']; ?>" target="_blank">
<span class="ccl-u-weight-bold"><?php echo $post['post_title']; ?></span>
<span>View <span class="ccl-b-icon arrow-right" aria-hidden="true"></span></span></a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php $post_type_count++; ?>
<?php endforeach; ?>
<?php else: //this is the end of ccl-l-row
//no results
?>
<div>Sorry, no search results found for "<?php echo get_search_query(); ?>"</div>
<?php endif; ?>
</div>
<?php //the_posts_navigation(); ?>
<div class="ccl-u-mt-3 ccl-u-mb-3">
<?php //the_posts_pagination( $pagination_args ); ?>
</div>
</div>
</div>
<?php
get_footer();