WooCommerce filter search by category and custom field

mokalovesoulmate picture mokalovesoulmate · Apr 28, 2014 · Viewed 9.8k times · Source

I want to ask how to create a woocommerce custom search form.

My search and filter form has 3 fields:

Category: I managed to pull woocommerce product category into my custom search html form with <select> tag:

<?php 
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77'));
foreach($catTerms as $catTerm) : ?>
    <option value="<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></option>
<?php endforeach; ?>

Filter By: (dropdown menu) by author:

I added extra field to woocommerce using woo_add_custom_general_fields_save() function on theme's functions.php, works properly.

Note: this is not 'custom field' we usually use on wordpress to add some more metadata, but this code below is to add more field on Product Data > General (on woocommerce).

function woo_add_custom_general_fields_save($post_id)
{
    $woocommerce_textinput = $_POST['_book_author'];
    if( !empty( $woocommerce_textinput ) )
    update_post_meta( $post_id, '_book_author', esc_html( $woocommerce_textinput ) );
}

By Title:

I managed to use this filter using http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle

Input text field:

This is for user to search by keyword.

So this is my complete custom html search form:

<form action="<?php echo site_url(); ?>/pm-bookstore/" method="GET">

<select name="">
    <?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
        <?php foreach($catTerms as $catTerm) : ?>
        <option value="<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></option>
    <?php endforeach; ?>                                            
</select>

<select name="">
    <option value="">By author</option>
    <option value="">By title</option>
</select>

<input type="text" placeholder="Search Book by Title, Author, ISBN..." name="s">
<button class="fa fa-search" type="submit"></button>

</form>

For search parameter I expect to able to pull them all. But I only able to use the ?s parameter (which is only product title).

I tried using another parameter, like, product_cat, tag_ID, but no success.

At the moment I only able to use

http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle

My expected result is:

http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle&category=categoryslug&author=authorname

or

http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle&category=categoryslug

How to make this search parameter working on woocommerce search?

Thank you.

Answer

Raunak Gupta picture Raunak Gupta · Aug 2, 2016

In your 'pm-bookstore' page use WP_Query to get the result.

// WP_Query arguments
$args = array (
    'name'                   => 'your title',
    'post_type'              => array( 'product' ),
    'post_status'            => array( 'publish' ),
    'tax_query'              => array(
             'taxonomy' => 'categories',
             'field'    => 'slug',
             'term'     =>  'your category slug'
     ),
    'meta_query'             => array(
        array(
            'key'       => '_book_author',
            'value'     => 'your author name',
            'compare'   => '=',
            'type'      => 'CHAR',
        ),
    ),
);

// The Query
$query = new WP_Query( $args );

I haven't tested it but it should work.