Replacing native search with WooCommerce Product search - wordpress

I'm running a theme name melokids, for a baby clothing store.
I'm struggling with trying to replace the theme's native search function with Woo Commerce, makes sense right?
Would appreciate if anybody would have any idea on this, Here's the current search function I believe:
<?php
if(!function_exists('melokids_header_search')){
function melokids_header_search($args=[]){
$args = wp_parse_args($args, ['icon' => 'fa fa-search']);
$header_search = melokids_get_opts('header_search','0');
if('0' === $header_search) return;
wp_enqueue_script('magnific-popup');
wp_enqueue_style('magnific-popup');
$unique_id = esc_attr( uniqid( 'search-form-' ) );
?>
<a href="#zk-header-search" class="mfp-search">
<span class="<?php echo esc_attr($args['icon']);?>"></span>
</a>
<div id="zk-header-search" class="mfp-hide">
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="<?php echo esc_attr($unique_id); ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Type something to search ...', 'placeholder', 'melokids' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
<button type="submit" class="search-submit"><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'melokids' ); ?></span></button>
</form>
</div>
<?php
}
}
?>

<form role="search" method="get" class="woocommerce-product-search form-inline" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="woocommerce-product-search-field-0" class="search-field form-control" placeholder="Search…" value="" name="s">
<button type="submit" id="searchsubmit" value="Search" class="btn">Search</button>
<input type="hidden" name="post_type" value="product">
</form>
try this

Related

get_product_search_form not showing products in archive_products layout

When i use get_product_search_form i get normal search results - to be precise, i get the products description.
Here's the product_searchform.php code:
<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="submit" class="search-button" value="">
<input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products…', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
<input type="hidden" name="post_type" value="product" />
</form>

Wordpress: Add orderby field in search form

I have created a WordPress custom search form for a particular custom post type. The results are showing up in an order other than newest posts first. I would like the search results to be displayed with the newest custom posts first. Is there a hidden field that I can add to the search form, so the results will show in date order DESC?
I tried:
<input type="hidden" name="orderby" value="date" />
and
<input type="hidden" name="orderby" value="post_date DESC" />
but neither worked.
Here is my search form code:
<div class="event-search-form">
<form method="get" class="searchform" action="<?php echo esc_attr( $action ); ?>"<?php wpex_aria_landmark( 'searchform' ); ?>>
<input type="hidden" name="post_type" value="ai1ec_event" />
<input type="hidden" name="orderby" value="date" />
<input type="hidden" name="order" value="DESC" />
<label>
<span class="screen-reader-text"><?php echo esc_html( $placeholder ); ?></span>
<input type="search" class="field" name="s" placeholder="<?php echo esc_attr( $placeholder ); ?>" />
</label>
<?php if ( defined( 'ICL_LANGUAGE_CODE' ) ) : ?>
<input type="hidden" name="lang" value="<?php echo( ICL_LANGUAGE_CODE ); ?>"/>
<?php endif; ?>
<?php do_action( 'wpex_searchform_fields' ); ?>
<button type="submit" class="searchform-submit"><span class="fa fa-search" aria-hidden="true"></span><span class="screen-reader-text"><?php esc_html_e( 'Submit', 'total' ); ?></span></button>
</form>
</div>

Wordpress search custom form

I have standard search form in on my WP.
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search', 'twentyeleven' ); ?></label>
<input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
</form>
How can I do this?
Simply create a searchform.php in your theme-folder.
Copy the php-tags from the standard code, to achieve similar behaviour. Also don't change the name-attributes, or it won't work.
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
See the example on this Codex page

WordPress Search Problems

I have researched this problem and still can't seem to figure out why my search function isn't working on a theme I'm working on. Below is my code that I have for my search.php and my searchform.php files. The search files were hardly modified from a boilerplate, blank WordPress theme. When I search for anything, it returns a 404 error. I have researched search's returning 404's and still can't find the solution. Blow is my code, any help would be greatly appreciated.
searchform.php----------------------------------
<form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get">
<label for="search"><object data='<?php bloginfo('template_directory');? >/images/input-search.svg'>
<img src='<?php bloginfo('template_directory');?>/images/input-search.png'>
</object></label>
<input type="search" name="search" />
</form>
search.php---------------------------------------
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<h2>Search Results</h2>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>No posts found.</h2>
<?php endif; ?>
You need to make sure that your search form is enabled in your functions.php file like so:
// ENABLES SEARCH FORM STUFF
function my_search_form( $form ) {
$form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
</div>
</form>';
return $form;
}
// ENABLES SEARCH FORM
add_filter( 'get_search_form', 'my_search_form' );
This is the code I use in my themes. Then this would call in my search form:
<?php get_search_form(); ?>
This is my searchform.php
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search' ); ?></label>
<input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search' ); ?>" />
</form>

Wordpress Custom Search by post_type

I've tried a couple of methods but I cannot seem to filter custom post_types from my search results and was hoping someone could help.
I have installed "Job Manager" and created 4 jobs which have a custom post_type = 'jobman_job'
I tried to create a manual search form and set a hidden value of post_type = jobman_job but it still returned all posts.
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
I then tried creating a custom search page and redirecting the search to this page as follows (i.e added page_id hidden field):
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="page_id" value="123" />
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
And then in the custom search page, I added the following code (as per wordpress guide - http://codex.wordpress.org/Creating_a_Search_Page) and I added the post_type of jobman_job to the query array:
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array('post_type' => 'jobman_job');
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
And it still displays all posts...
What am I doing wrong? I have checked the post_type column in the wp_posts table and I have 4 unique entries...so they are there...
Any Insight?
As codex explains, after getting new data you need to replace the loop with your new data, like in this example
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
Displaying posts from custom query
I simply left the html as is:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
and added the following to my functions.php
function mySearchFilter($query) {
if (isset($_GET['post_type']) && $_GET['post_type'] == 'jobman_job') {
$post_type = 'jobman_job';
} else {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');

Resources