Combine Terms and Posts in Search Result with WP_Query

Combine Terms and Posts in Search Result with WP_Query

Approach

As we know the default WordPress search form searches for posts by the title and displays the result according to the keywords entered. But I think the search should be doing more. The search should be more effective as users don’t necessarily know what they are searching and how the search mechanism works.

So, to make the search even more effective, why not bring the terms and taxonomies into action. I think if taxonomies and posts are combined the search will produce better results.

In this tutorial, we will be breaking that search query into 2 parts.

The first part will search for the matching terms from a given taxonomy and extract the posts from them and the second part will search for the posts that matches the search keywords entered. The results from both of these queries will be combined to list out the maximum posts found according to the search keywords.

First Part:

Here, the terms from a taxonomy ‘product_categories’ is searched on the basis of the keyword i.e $search_text.
Then the posts that are assigned under those terms are stored in an array $get_postids.

$search_text = 'test'; //search keyword
$like_post_ids = array();
$get_postids = array();
$tax_args = array(
'taxonomy' => apply_filters('my_tax_filter','product_categories'), // taxonomy name
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'all',
'name__like' => $search_text
);
echo '<h3>Search result for %'.$search_text.'%</h3>';
$termsids = get_terms( $tax_args );
if( $termsids )
{
foreach ( $termsids as $key => $value ) {
$get_termsids[] = $value->term_id;
}
if($get_termsids)
{
$args =
array(
'posts_per_page' => -1,
'post_type' => apply_filters('my_post_filter','product'),
'tax_query' => array(
array(
'taxonomy' => apply_filters('my_tax_filter','product_categories'),
'field' => 'term_id',
'terms' => $get_termsids,
)
)
);
}
$post_ids = get_posts( $args );
if( $post_ids )
{
foreach ( $post_ids as $key => $value ) {
$get_postids[] = $value->ID;
}
}
}

Second Part:

Here, the posts are searched on the basis of the keyword i.e $search_text.
Then those post ids are stored in an array $like_post_ids.

$like_args = array(
'posts_per_page' => -1,
'post_type' => apply_filters('my_post_filter','product'),
's' => $search_text,
);
$query = new WP_Query( $like_args );
global $post;
if ($query->have_posts()){
while ( $query->have_posts() ) {
$query->the_post();
$like_post_ids[] = $post->ID;
} //end while
}

Now, a new array is formed merging two arrays $like_post_ids and $get_postids and having unique post ids.

$array = array_unique( array_merge( $like_post_ids,$get_postids ) );

if( sizeof($array)>0 ) //if array has some post ids
{
$args = array(
'post_per_page' => -1,
'post_type' => apply_filters('my_post_filter','product'),
'post__in' => $array
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
}
wp_reset_query();
}
else{
echo 'No result found';
}

This technique can be also used in

pre_get_posts

to show even better search result in the search result page.

Hence, search can be made even more effective, bringing the terms and taxonomies into play. I think if taxonomies and posts are combined the search will produce better results.

Leave comment

Your email address will not be published. Required fields are marked with *.