I am using get_posts to get a list of posts that match a search keyword, the problem is that I the get_posts
‘s s
parameter does not search tags by default and using tag_slug__in
will not work if the keyword is found in the title and not in a tag.
The conditions of my search would be:
- Return post if keyword exists in Title
- Return post if keyword exists in Content
- Return post if keyword is a tag associated with the post.
Any ideas would be fantastic. I tried the “Search Everything” plugin, but that only seems to work on WordPress’ default Search Feature.
Code below is a simplified version of what I have attempted, not this does not satisfy all 3 criteria.
<?php
/* Set global query parameters */
$image_args = array(
'posts_per_page' => (isset($_GET['show_all']) && $_GET['show_all'] == 'true')? 100000 : 20,
'post_type' => 'attachment',
'post_mime_type' => array('image/jpeg', 'image/png'),
'meta_key' => 'language',
'meta_value' => "(^".$languages."$|"".$languages."";)",
'meta_compare' => 'REGEXP',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'media_category',
'field' => 'term_id',
'terms' => array($gallery_filter, $hotel->term_id),
'operator' => 'AND',
),
),
);
/* If page numbert given, add offet */
if(!empty($page_no))
$images_args['offset'] = 20*((int)$page_no-1);
/* If search term submitted, add it to the s parameter */
if(isset($_GET['search'])){
$image_args['tag_slug__in' = explode(" ", $_GET['search']);
$image_args['s'] = urldecode($_GET['search_term']);
}
you can use your own query . below is an example
I have used
wpdb
query to fetch the result. Join the posts and taxonomy table and then get the distinct result by$search_title
. Hope this will work. 🙂Check out the Post Clauses filters WordPress offers. It allows you to plug in MySQL Statements directly to the WP query.