How to search coupons by description in WP Coupons and Deals plugin
-
There is a good plugin for creating and displaying coupons database. It's called WP Coupons and Deals. But it has one problem - a lack of good search within coupons.
Fortunately, with WPFTS plugin flexibility it's pretty simple to add this functionality with just some lines of code!
Look here: actually, all we need is to put coupon description (stored in the custom post meta) to the Search Index, and additionally, we need to add coupon custom post type to the list of searchable post types.
And here is the ready code!
add_filter('wpfts_index_post', function($index, $post) { if ($post && $post->post_type == 'wpcd_coupons') { $meta = get_post_meta($post->ID, 'coupon_details_description', true); $index['post_content'] .= $meta; } }); add_action('pre_get_posts', function(&$wpq) { if ($wpq->is_main_query() && $wpq->is_search) { $wpq->set('post_type', array('wpcd_coupons', 'post', 'page')); } });It was simple, do you agree?
Oh, and one more good thing: you don't need WPFTS Pro to get this code working!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login