AJAX custom search for a wordpress

asked May 7, 2012 in Website Builder by darkshark (1,500 points)
I have a wordpress  website and I created an AJAX  custom powered search to search for my custom post. The search needs to find matching products by each word typed into the search box if any of the words match the post , the post title,  or the post meta fields.
This  function is called when a user makes a research
function display_all_products() {

            global $term;
            $term = trim(strip_tags($_POST['s']));

                    add_filter( 'posts_where' , 'product_posts_where' );

                $args = array(
                    'posts_per_page' => 50,
                    'paged' => 1,
                    'post_type' => 'product',
                    'order' => 'DESC',

                $the_query = new WP_Query( $args );

                remove_filter('posts_where', 'product_posts_where');

                if($the_query->have_posts()) {
                    while ( $the_query->have_posts() ) {

                        $pid = get_the_ID();
                        $refno = get_post_meta( $pid, 'refno', true );
                        $yr = get_post_meta( $pid, 'yr', true );
                        $pieces = get_post_meta( $pid, 'pieces', true );
                        $figures = get_post_meta( $pid, 'figures', true );


                        <div class="my_box3">
                            <h3><?php the_title(); ?></h3>
                            <div class="padd10"><?php the_post_thumbnail(); ?></div>
                            <div class="padd10">
                                    <li> <?php _e('Referance Number', 'AuctionTheme');?>: <?php  echo $refno; ?></li>
                                    <li> <?php _e('Year', 'AuctionTheme'); ?>: <?php echo $yr; ?></li>
                                    <li> <?php _e('Pieces', 'AuctionTheme'); ?>: <?php echo $pieces; ?></li>
                                    <li> <?php _e('Figures', 'AuctionTheme'); ?>: <?php echo $figures; ?></li>
                            <label for="product">Select Product: <input type="radio" name="product" value="<?php the_ID(); ?>" /></label>




    add_action('wp_ajax_show_all_products', 'display_all_products');
    add_action('wp_ajax_nopriv_show_all_products', 'display_all_products');

    function product_posts_where( $where ) {

            global $wpdb, $term;
            $searchTerms = explode(' ', $term);

            $i = 1;
            $where .=" AND (";

            foreach ($searchTerms as $word) {
                if($i === 1) {
                    $where .= " ({$wpdb->posts}.post_title LIKE '%$word%' OR {$wpdb->posts}.post_excerpt LIKE '%$word%')";
                } else {
                    $where .= " OR ({$wpdb->posts}.post_title LIKE '%$word%' OR {$wpdb->posts}.post_excerpt LIKE '%$word%')";

            $where .=")";

        return $where;

I am not able to get worpress to search the database for posts and postmeta at the same time and combine the results. It doesn't work if you want to search for posts with matching post title or matching metadata in the same query.

can anyone please help me with this?
Thanks in advance

2 Answers

0 like 0 dislike
answered May 7, 2012 by moon_tag (1,380 points)
Create a search form that contains more than 1 field, so in your case, you will need two fields and two queries at the same time, then return the resulting posts that matches the search.
0 like 0 dislike
answered May 7, 2012 by lightSpeed (2,420 points)
try this code
Use this code
The HTML for the search boxes:
<label for="product_select"><?php echo __('Search our database for your product')?></label><br/>
        <label for="my-s">Keyword: </label><input type='text' name='my-s' id='my-s' /><br/>
        <label for="ref">Reference/Catalog Number: </label><input type='text' name='ref' id='ref' /><br/>
        <label for="yr">Year </label><input type='text' name='yr' id='yr' /><br/>
        <input type="submit" name="product_search_submit" id="product