Last active
January 26, 2018 17:24
-
-
Save certainlyakey/9725345 to your computer and use it in GitHub Desktop.
Wordpress - Custom search including custom fields
This file contains hidden or 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 $posttype = 'registry'; | |
$posttype_obj = get_post_type_object($posttype); | |
$search_posttype_label = $posttype_obj->labels->search_items; | |
$term = $wp_query->queried_object; | |
// $term_based_on_posttype = get_object_taxonomies($posttype); | |
?> | |
<!-- change all ids in form to prevent conflict with default site search form --> | |
<form role="search" method="get" id="searchform_<?php echo $posttype; ?>" class="searchform" action="search-<?php echo $posttype; ?>.php"> | |
<div> | |
<label class="screen-reader-text" for="s_<?php echo $posttype; ?>"><?php echo $search_posttype_label; ?></label> | |
<input type="text" value="" name="s" id="s_<?php echo $posttype; ?>"> | |
<input type="hidden" name="post_type" value="<?php echo $posttype; ?>"> | |
<!-- if on taxonomy page --><input type="hidden" name="term" value="<?php echo $term->slug; ?>"> | |
<input type="submit" id="searchsubmit_<?php echo $posttype; ?>" value="OK"> | |
</div> | |
</form> |
This file contains hidden or 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 | |
//Custom search in both post title/content and custom fields, may be narrowed to a certain post type and/or category/tax (sent as URL parameters) | |
//Include <input type="hidden" name="post_type" value="[post-type]"> in search form code to send URL parameters | |
//May be used in custom search template (for example, search-[posttype].php) | |
//Original code from http://www.deluxeblogtips.com/2012/04/search-all-custom-fields.html | |
global $wpdb; | |
// If you use a custom search form | |
// $kw_unesc = sanitize_text_field( $_GET['s_registry'] ); | |
$taxname = 'regcat'; | |
$kw_unesc = get_search_query(); | |
$posttype = sanitize_text_field( $_GET['post_type'] ); | |
$term = sanitize_text_field( $_GET['term'] ); | |
$termTitle = get_term_by('slug',$term,$taxname); | |
if (!preg_match('/\s/',$kw_unesc)) { | |
$keyword = '%' . like_escape( $kw_unesc ) . '%'; | |
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%s'", $keyword ) ); | |
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_title LIKE '%s' OR post_content LIKE '%s'", $keyword, $keyword ) ); | |
} else { | |
$keywords = array(); | |
$post_ids_meta = array(); | |
$post_ids_post = array(); | |
$keywords_unescaped = explode(" ", $kw_unesc); | |
$mysqlquery_postmeta = "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%s'"; | |
$mysqlquery_postcontent = "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_title LIKE '%s' OR post_content LIKE '%s'"; | |
foreach ($keywords_unescaped as $kw_unesc) { | |
$keywords[] = '%' . like_escape( $kw_unesc ) . '%'; | |
} | |
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( $mysqlquery_postmeta, $keywords ) ); | |
$post_ids_post = $wpdb->get_col( $wpdb->prepare( $mysqlquery_postcontent, $keywords, $keywords ) ); | |
} | |
$post_ids = array_merge( $post_ids_meta, $post_ids_post ); | |
$args = array( | |
'post_type' => $posttype | |
,'post__in' => $post_ids | |
,'paged' => max( get_query_var( 'paged' ), 1 ) | |
); | |
if ($term) { | |
$args[$taxname] = $term; | |
} | |
$query = new WP_Query( $args ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment