• Resolved Bruno Apr

    (@outpacer)


    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)
  • Plugin Author Mikko Saari

    (@msaari)

    The main problem is that the add_filter() calls that hook in the functions are inside the functions, so the function never gets added in the first place.

    Just from your code, I don’t understand what you are trying to achieve here. Can you please explain in more detail what you are doing? If you want new content to be added in the posts, it’s usually better to do it once in indexing, instead of doing it every time someone searches. If you want to find parent posts based on content in the child posts, it’s best to index the child post content for the parent post.

    Thread Starter Bruno Apr

    (@outpacer)

    Hi Mikko, thank you for a such quick reply and comments.

    I would like with a Relevanssi search placed in the parent post view check also for data in the related child posts, and after display in results parent posts that are related to child posts found form the search.

    I will try to provide more details. The search will be looking for specific fields in parent post (for example fields “aa”, and “ab”) for specific fields in related child post (for example fields “bc” and “bd”). As such when user enter search values that are related to child post fields “bc” or “bd” the search results should display not a child post but a parent post related to the child post with fields “bc” or “bd”. Also, if a user search values that are related to the parent post it should display related parent posts. For example parent post is brand and child post is a car type (SUV, sedan etc). We have a view of brands that include filter that search in fields of brand names and car types. If user search for “SUV”, we should display all brands that have “SUV” in related child post field. Sorry for any confusion, please let me know if I should provide details differently.

    Plugin Author Mikko Saari

    (@msaari)

    Ok, that’s what I thought you were doing. In this case, the correct solution is to index the child post fields for the parent post. That way you only have to do it once, not every time someone searches.

    The hook you’re looking for is relevanssi_content_to_index. You can use it to add content to posts as they’re indexed. Create a function that for each post finds all the child posts, fetches the content you want to add from the child posts, and adds it to the parent post.

    Here’s an example.

    Thread Starter Bruno Apr

    (@outpacer)

    Hi Mikko, thank you for your advice and example.

    Have a great day

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.