First let me say I've hardly ever posted on stackoverflow so I hope I'm in the right section.
I have a Wordpress site with Woocommerce for my client. I want the search bar to NOT search for products and instead search for pages. It's important to ignore all products because we don't order products from product pages, instead I have collections of order forms on multiple pages that are titled with the product name. Can we change the search bar so it picks up on just the pages and not products?
I'm comfortable with working in the functions.php or duplicating Woocommerce files in my child theme. Any help is much appreciated!
[** EDITED/UPDATE **] I now have this:
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
}
return $query;
}
add_filter('pre_get_posts', 'filter_search');
function __search_by_title_only( $search, &$wp_query ){
global $wpdb;
if ( empty( $search ) )
return $search; // skip processing - no search term in query
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term{{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
$search .= " AND ($wpdb->posts.post_type = 'page') ";
return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
Which is working perfectly. It searches for pages not products and it searches for the search word in a title instead of the content. Now my only request and issue is I need to make it output the page LINK not the page CONTENT. (It outputs the pure content of the page, one after another)
[** EDITED/UPDATE #2 **]
I achieved the same result as above by removing all of that code and copying product-searchform.php (from woocommerce) into my child theme (in a folder called woocommerce) and changing:
<input type="hidden" name="post_type" value="product" />
to
<input type="hidden" name="post_type" value="page" />
This achieves the same affect of querying only pages and not products, as well as searching the keyword in the page titles instead of searching in the page content. Now I am still getting the same output of it displaying the page content and which I only want it to display the link to the page, and not the content. Any ideas?
[** EDITED/UPDATE 3 **] Okay, I solved this.
Next step is to copy search.php into your child theme and add some code. Add:
$s=get_search_query();
$args = array('s' =>$s);
// The Query
$the_query = new WP_Query( $args );
Right before
if ( have_posts() ) : ?>
Then add:
while ( $the_query->have_posts() ) {
$the_query->the_post();?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li><?php
}
Right before:
get_template_part( 'loop' );
And comment that get_template_part('loop'); part out.
Try to add this code in functions.php
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');