How to Query Posts and Postmeta by keywords (for custom search)
-
Hi Everyone,
I’ve been tearing my hair out trying to solve this so I hope someone can help.
I’m building a custom search to search a custom post type title and excerpt and it’s associated meta at the same time. I have it nearly there but i just can’t get it to work properly.
Here is what I have in my functions.php (this is called by an AJAX function and displayed when the user searches)
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 have tried using a seperate query to query the postmeta, but that means you can get multiple results of the same product.
I have tried adding
'meta_value' =>$term
to the WP_Query array but that doesn’t work as the posts_where filter gets in the way and i get no results.I have tried adding
OR {$wpdb->postmeta}.meta_value LIKE '%$word%'
to the end of the statement in the posts_where filter but that causes no results to show.I feel that the answer lies in adding to the posts_where filter but I cannot work it out.
Or perhaps a completely different approach – any ideas are appreciated!
If anyone can please help, i’ve looked at so many solutions but just can’t work out how to to do this.
Thanks in advance,
Ralph
Ralph
- The topic ‘How to Query Posts and Postmeta by keywords (for custom search)’ is closed to new replies.