Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Forked from phpprousa/page-state-search.php
Created May 20, 2016 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/3bdb9e67dd74e76d045e99c9f72441b8 to your computer and use it in GitHub Desktop.
Save jchristopher/3bdb9e67dd74e76d045e99c9f72441b8 to your computer and use it in GitHub Desktop.
<?php
session_start();
/* Template Name: SearchWP State Search Results */
global $post;
//get the query
$query = isset( $_REQUEST['t'] ) ? sanitize_text_field( $_REQUEST['t'] ) : '';
$stateId = isset($_REQUEST['stateId']) ? sanitize_text_field( $_REQUEST['stateId'] ) : '';
if($stateId == '*') {
$stateId = false;
}
$stateName = ($stateId != '') ? get_cat_name($stateId) : '';
//get the pagination
$swppg = isset( $_REQUEST['swppg'] ) ? absint( $_REQUEST['swppg'] ) : 1;
//get the children of the "News" category
$newsChildren = get_term_children(NEWS, 'category');
if($stateId) {
$taxArray = array('relation' => 'AND');
//setup to only search this state
$taxArray[] = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $stateId
);
}
$childrenArray = array('relation' => 'OR');
foreach($newsChildren as $childId) {
$childrenArray[] = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $childId
);
}
$taxArray[] = $childrenArray;
$_SESSION['test_search'] = $childrenArray;
$_SESSION['test_query'] = $query;
if(class_exists('SWP_Query')) {
$engine = 'state_search';
$swp_query = new SWP_Query(
array(
's' => $query,
'post_type' => array('post','page','attachment'),
'page' => $swppg,
'tax_query' => array(
$taxArray
)
)
);
print_r( $swp_query );
// set up pagination
$pagination = paginate_links( array(
'format' => '?swppg=%#%',
'current' => $swppg,
'total' => $swp_query->max_num_pages,
) );
$_SESSION['test_results'] = $swp_query->posts;
}
get_header();?>
<div id="container" class="searchresults">
<div id="content">
<?php if(have_posts() ): ?>
<h1 class="page-title" style="padding-top:10px;"><?php echo ($stateName.' News Search Results for: '); ?><span><?php echo esc_html($query);?></span></h1>
<div>Total Found: <?php echo count($swp_query->posts); ?></div>
<?php endif; ?>
<?php if ( ! empty( $query ) && isset( $swp_query ) && ! empty( $swp_query->posts ) ) {
$swp_query->posts = ajs_presort($swp_query->posts);
foreach ( $swp_query->posts as $post ) {
if (is_user_logged_in()) {
$permalink = get_permalink();
if( 'attachment' == get_post_type() ) {
// if the result is a PDF, link directly to the file not the attachment page
$permalink = wp_get_attachment_url( $post->ID );
}
?>
<!-- RESULT START -->
<?php
//echo( get_post_type().'<br>');
//echo( array_pop($weights)['weight']);
//echo('<br>');
if('page' == get_post_type() && $categoriesTitleIssued == false){
$categoriesTitleIssued = true;
?>
<div class="search-header">
<h2>Categories</h2>
</div>
<?php }?>
<?php
if('post' == get_post_type() && $datapackageTitleIssued == false /*&& $categoriesTitleIssued == true*/){
$datapackageTitleIssued = true;
?>
<div class="search-header">
<h2 >Data Packages</h2>
</div>
<?php }?>
<div class="search-item" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="search-entry-title">
<?php
$span = '';
if (get_post_mime_type(get_the_ID())) {
$type = get_post_mime_type(get_the_ID());
switch($type) {
case 'image/jpeg':
$span = '<span class="jpg-icon"></span>';
break;
case 'image/png':
case 'image/png':
$span = '<span class="png-icon"></span>';
break;
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
case 'application/msword':
$span = '<span class="word-icon"></span>';
break;
case 'application/vnd.ms-powerpoint':
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
$span = '<span class="ppt-icon"></span>';
break;
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
$span = '<span class="xlsx-icon"></span>';
break;
case 'application/pdf':
$span = '<span class="pdf-icon"></span>';
break;
case 'text/csv':
$span = '<span class="csv-icon"></span>';
break;
case 'application/zip':
$span = '<span class="zip-icon"></span>';
break;
default:
$span = '<span class="file-icon"></span>';
}
}
echo $span;
?>
<a href="<?php echo $permalink ?>" title="<?php printf( __('Permalink to %s', 'hbd-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php if ( $post->post_type == 'post' ) : ?>
<div class="entry-meta">
<span class="meta-prep meta-prep-author"><?php _e('By ', 'hbd-theme'); ?></span>
<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'hbd-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>
<span class="meta-sep"> | </span>
<span class="meta-prep meta-prep-entry-date"><?php _e('', 'hbd-theme'); ?></span>
<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>
</div><!-- .entry-meta -->
<?php endif; ?>
<?php if ( $post->post_type == 'post' ) : ?>
<div class="entry-summary">
<?php the_excerpt( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'hbd-theme' ) ); ?>
<?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'hbd-theme' ) . '&after=</div>') ?>
</div><!-- .entry-summary -->
<?php endif; ?>
<?php if ( $post->post_type == 'post' || $post->post_type == 'page') : ?>
<div class="entry-utility">
<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'hbd-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>
</div><!-- #entry-utility -->
<?php endif; ?>
<span class="search-diags">[Debug Type: <?php echo $post->post_type ?> <?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged: ', 'hbd-theme' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>]</span>
</div><!-- #post-<?php the_ID(); ?> -->
<?php }
else {
?>
<!-- Publicly viewable posts -->
<div class="search-item" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="search-entry-title"><a href="<?php echo get_permalink() ?>" title="<?php printf( __('Permalink to %s', 'hbd-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="entry-summary">
<p><?php echo clip_to_paragraph_end($post->post_excerpt); ?></p>
<?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'hbd-theme' ) . '&after=</div>') ?>
</div><!-- .entry-summary -->
<div class="entry-utility">
<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'hbd-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>
</div>
</div>
<?php }
}
wp_reset_postdata();
// pagination
if ( $swp_query->max_num_pages > 1 ) { ?>
<div class="navigation pagination" role="navigation">
<h2 class="screen-reader-text">Posts navigation</h2>
<div class="nav-links">
<?php echo wp_kses_post( $pagination ); ?>
</div>
</div>
<?php }
} else {?>
<div id="post-0" class="post no-results not-found">
<h2 class="search-entry-title"><?php _e( 'Nothing Found', 'hbd-theme' ) ?></h2>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'hbd-theme' ); ?></p>
<?php echo hma_state_search_form($stateId); ?>
</div><!-- .entry-content -->
</div>
<?php } ?>
</div><!-- end #content -->
</div><!-- end #container -->
<?php get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment