Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:11
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save codehooligans/fbf09a726e75a960b395 to your computer and use it in GitHub Desktop.
Codehooligans.com: Media-Tags Search template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Template Name: Media-Tags Search | |
*/ | |
// Get the Page permalink. Used later when displaying date links | |
$page_permalink = get_permalink(); | |
?> | |
<?php get_header(); ?> | |
<div id="main-content" class="main-content"> | |
<div id="primary" class="content-area"> | |
<div id="content" class="site-content" role="main"> | |
<form method="get"> | |
<h3>Search for posts</h3> | |
<?php | |
$taxonomies = array(); | |
$taxonomies[] = get_taxonomy(MEDIA_TAGS_TAXONOMY); | |
if(!empty($taxonomies)) { | |
$options = array(); | |
foreach($taxonomies as $taxonomy) { | |
if(!empty($taxonomy)) { | |
$options[] = sprintf("\t".'<option value="%1$s">%2$s</option>', $taxonomy->name, $taxonomy->labels->name); | |
} | |
} | |
if(!empty($options)) { | |
echo sprintf('<select name="search-taxonomy" id="search-taxonomy">'."\n".'$1%s'."\n".'</select>', join("\n", $options)); | |
} | |
} | |
?> | |
<input type="text" name="search-text" id="search-text" value="<?php | |
if ((isset($_GET['search-text'])) && (!empty($_GET['search-text']))) { | |
echo esc_attr($_GET['search-text']); | |
} ?>" /> | |
<input type="submit" name="search" id="search" value="Search" /><br /> | |
<input type="radio" name="search-join" id="search-join-and" value="AND" <?php | |
if ((!isset($_GET['search-join'])) || ($_GET['search-join'] == 'AND')) { | |
echo ' checked="checked" '; | |
} ?> /> <label for="search-join-and">AND</label> <input type="radio" name="search-join" id="search-join-or" value="OR" <?php | |
if ((isset($_GET['search-join'])) && ($_GET['search-join'] == 'OR')) { | |
echo ' checked="checked" '; | |
} ?> /> <label for="search-join-or">OR</label> | |
</form> | |
<?php | |
if ((isset($_GET['search-taxonomy'])) && (!empty($_GET['search-taxonomy'])) | |
&& (isset($_GET['search-text'])) && (!empty($_GET['search-text']))) { | |
echo "_GET<pre>"; print_r($_GET); echo "</pre>"; | |
$search_terms = explode(',', $_GET['search-text']); | |
foreach($search_terms as $idx => $term) { | |
$search_terms[$idx] = "'". sanitize_title($term) ."'"; | |
} | |
$args = array( | |
'post_type' => 'attachment', | |
'post_status' => 'inherit', | |
'orderby' => 'menu_order', | |
'order' => 'ASC', | |
'paged' => get_query_var( 'paged' ), | |
'tax_query' => array( | |
array( | |
'taxonomy' => MEDIA_TAGS_TAXONOMY, | |
'field' => 'slug', | |
'terms' => $search_terms, | |
'operator' => esc_attr($_GET['search-join']) | |
) | |
) | |
); | |
//echo "args<pre>"; print_r($args); echo "</pre>"; | |
$media_tags_query = new WP_Query( $args ); | |
// The Loop | |
if ( $media_tags_query->have_posts() ) { | |
echo '<ul>'; | |
while ( $media_tags_query->have_posts() ) { | |
$media_tags_query->the_post(); | |
echo '<li>' . get_the_title() . '<br />'. wp_get_attachment_image($post->ID, 'thumbnail') .'</li>'; | |
} | |
echo '</ul>'; | |
media_tags_paging_nav($media_tags_query); | |
} else { | |
?><p>No image found matching your search</p><?php | |
} | |
/* Restore original Post Data */ | |
wp_reset_postdata(); | |
} | |
?> | |
</div><!-- #content --> | |
</div><!-- #primary --> | |
<?php get_sidebar( 'content' ); ?> | |
</div><!-- #main-content --> | |
<?php | |
get_sidebar(); | |
get_footer(); | |
function media_tags_paging_nav( $wp_query ) { | |
global $wp_rewrite; | |
// Don't print empty markup if there's only one page. | |
if ( $wp_query->max_num_pages < 2 ) { | |
return; | |
} | |
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1; | |
$pagenum_link = html_entity_decode( get_pagenum_link() ); | |
$query_args = array(); | |
$url_parts = explode( '?', $pagenum_link ); | |
if ( isset( $url_parts[1] ) ) { | |
wp_parse_str( $url_parts[1], $query_args ); | |
} | |
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link ); | |
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%'; | |
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; | |
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; | |
// Set up paginated links. | |
$links = paginate_links( array( | |
'base' => $pagenum_link, | |
'format' => $format, | |
'total' => $wp_query->max_num_pages, | |
'current' => $paged, | |
'mid_size' => 1, | |
'add_args' => array_map( 'urlencode', $query_args ), | |
'prev_text' => __( '← Previous', 'twentyfourteen' ), | |
'next_text' => __( 'Next →', 'twentyfourteen' ), | |
) ); | |
if ( $links ) : | |
?> | |
<nav class="navigation paging-navigation" role="navigation"> | |
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1> | |
<div class="pagination loop-pagination"> | |
<?php echo $links; ?> | |
</div><!-- .pagination --> | |
</nav><!-- .navigation --> | |
<?php | |
endif; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment