How to hook in the post__in argument
-
Hi, I am trying to use Relevanssi search in a parent post type view created by Toolset. The search should search data in custom fields of a parent post as well as a child a post. The child post type has a nested view in the parent post type view.
Below you can see a custom code solution to do a search in parent and child post and merge results together but it doesn’t work. Apparently, there should be another way to hook in the post__in argument. Would you be kind to take a look and advice?
function func_rlv_adjust_search( $query ) {
? if(isset($_GET['wpv_post_search']) and $_GET['wpv_post_search']!='') {
? ? remove_filter( 'relevanssi_modify_wp_query', 'func_rlv_adjust_search' );
? ?$args ?= array(
? ? 's' ? ? ? ? ? => $_GET['wpv_post_search'],
? ? 'post_types' ?=> array('parent'),
? ? 'fields' => 'ids',
? ? 'relevanssi' ?=> true,
? );
$parent_query = new WP_Query( $args );
? $args ?= array(
? ? 's' ? ? ? ? ? => $_GET['wpv_post_search'],
? ? 'post_types' ?=> array('child'),
? ? 'fields' => 'ids',
? ? 'relevanssi' ?=> true,
? );
$child_query = new WP_Query( $args );
? $child_posts = array();
? foreach($child_query->posts as $k=>$v):
? ? ?$child_posts[] = toolset_get_related_post($v,'parent-to-child');
? endforeach;
? $final_res = array_merge($parent_query->posts,$child_posts);
? ?$query->query_vars['post__in'] = ?join(",",$final_res);
? // relevanssi_do_query( $query );
? add_filter( 'relevanssi_modify_wp_query', 'func_rlv_adjust_search' );
? ? ?$query->query_vars['post__in'] = ?join(",",$final_res);
? ? $query->set('post__in',join(",",$final_res));
}
// ?return $query;
}
//add_filter( 'relevanssi_results', 'rlv_rating_weights');
function rlv_rating_weights( $results ) {
? remove_filter( 'relevanssi_results', 'rlv_rating_weights');
? ?$child_childs = do_shortcode('[wpv-view name="get-related-child-posts" cached="off"]');
? ?$child_childs = explode(",",trim( $child_childs));
? ?$final = array_merge($results,$child_childs);
? $final_results = array();
? foreach($final as $k=>$v):
? ? $final_results[$v] = 1;
? endforeach;
? //add_filter('relevanssi_results', 'rlv_rating_weights');
? return $final_results;
}
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.