Can you show me your custom template? Are you using the global $wp_query or running a custom query? If you run a custom query, ensure you set the relevanssi query parameter to true.
Thread Starter
remidk
(@remidk)
I’m using a new WP_query. Here the code :
$query_args = array(
'post_type' => array('recette'),
's' => $s,
'posts_per_page' => 15,
'relevanssi' => true
);
$query = new WP_query( $query_args );
And for testing :
if ( $query->have_posts() ) {
echo $query->found_posts;
while ( $query->have_posts() ) {
$query->the_post();
?>
<h2>Display test</h2>
<?php
}
wp_reset_query();
}
I also tried with relevanssi_do_query( $query );
Looks good to me. Does the same search return results if you do it with the Relevanssi admin search (Dashboard > Admin search)?
Thread Starter
remidk
(@remidk)
I tried and it works well
The code you have should work. Is $query->posts empty? What about the results array from relevanssi_hits_filter? Or relevanssi_hits_to_show? Figuring out where the results go missing can help solve the problem.
Thread Starter
remidk
(@remidk)
$query->posts is always empty. If I try to print in relevanssi_hits_filter, I have this :
Array ( [0] => WP_Post Object ( [ID] => 187 [post_author] => 1 [post_date] => 2024-08-22 09:45:13 [post_date_gmt] => 2024-08-22 07:45:13 [post_content] => [post_title] => fromage [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => fromage [to_ping] => [pinged] => [post_modified] => 2024-08-22 09:45:14 [post_modified_gmt] => 2024-08-22 07:45:14 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/wordpress/?post_type=recettes&p=187 [menu_order] => 0 [post_type] => recettes [post_mime_type] => [comment_count] => 0 [filter] => raw [relevance_score] => 5 ) [1] => WP_Post Object ( [ID] => 130 [post_author] => 1 [post_date] => 2024-08-02 14:26:06 [post_date_gmt] => 2024-08-02 12:26:06 [post_content] => [post_title] => [test] fromage vegan [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => test-fromage-vegan [to_ping] => [pinged] => [post_modified] => 2024-08-02 14:26:06 [post_modified_gmt] => 2024-08-02 12:26:06 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/wordpress/?post_type=recettes&p=130 [menu_order] => 0 [post_type] => recettes [post_mime_type] => [comment_count] => 0 [filter] => raw [relevance_score] => 5 ) [2] => WP_Post Object ( [ID] => 51 [post_author] => 1 [post_date] => 2024-07-29 17:56:33 [post_date_gmt] => 2024-07-29 15:56:33 [post_content] => [recette_infos][recette_details][searchform post_types="recettes"] [post_title] => test [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => test [to_ping] => [pinged] => [post_modified] => 2024-08-22 17:12:57 [post_modified_gmt] => 2024-08-22 15:12:57 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/wordpress/?post_type=recettes&p=51 [menu_order] => 0 [post_type] => recettes [post_mime_type] => [comment_count] => 0 [filter] => raw [relevance_score] => 2 ) ) Array ( )
I didn’t find relevanssi_hits_to_show in the documentation. Do you have a link to this one ?
Oh, there’s a gap in the documentation. It’s basically the same as relevanssi_hits_filter, but the filtered array only has the posts that are displayed on the one page of results, when relevanssi_hits_filter has all the results found. It’s the last filter Relevanssi uses, so if you have results there, the problem is not Relevanssi.
Thread Starter
remidk
(@remidk)
I tried something like that :
add_filter( 'relevanssi_hits_to_show', 'rlv_hits_filter' );
function rlv_hits_filter( array $hits ) : array {
$kept_posts = array();
foreach ( $hits[0] as $_post ) {
$kept_posts[] = $_post;
}
print_r($kept_posts);
$hits[0] = $kept_posts;
return $hits;
}
And it looks like we have some results :
5112024-07-29 17:56:332024-07-29 15:56:33[recette_infos][recette_details][searchform post_types="recettes"]testpublishclosedclosedtest2024-08-22 17:12:572024-08-22 15:12:570http://localhost/wordpress/wordpress/?post_type=recettes&p=510recettes0raw6Array ( [0] => 51 [1] => 1 [2] => 2024-07-29 17:56:33 [3] => 2024-07-29 15:56:33 [4] => [recette_infos][recette_details][searchform post_types="recettes"] [5] => test [6] => [7] => publish [8] => closed [9] => closed [10] => [11] => test [12] => [13] => [14] => 2024-08-22 17:12:57 [15] => 2024-08-22 15:12:57 [16] => [17] => 0 [18] => http://localhost/wordpress/wordpress/?post_type=recettes&p=51 [19] => 0 [20] => recettes [21] => [22] => 0 [23] => raw [24] => 6 )
I will try to check my child theme. Thanks for your help ! 🙂