Web Hosting Geeks | Web Hosting Experts
Need help? Call us 1(866)TOP-HOST

14120 reviews 1680 hosting providers

Questions & Answers

AJAX custom search for a wordpress

May 7, 2012 by darkshark
Greetings
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']));

                if(!empty($_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() ) {
                        $the_query->the_post();

                        $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">
                                <ul>
                                    <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>
                                </ul>
                            <label for="product">Select Product: <input type="radio" name="product" value="<?php the_ID(); ?>" /></label>
                        </div>

                        <?php
                    }
                }

                wp_reset_query();
                //wp_reset_postdata();

            die();
    }

    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%')";
                }
                $i++;
            }

            $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 votes
May 7, 2012 by moon_tag
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 votes
May 7, 2012 by lightSpeed
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