-
-
Save dexit/42a72c4aa96007ec64d3093a23fe4ef1 to your computer and use it in GitHub Desktop.
Apparently SearchWP/Relevanss and Elementor doesn't play well together with Elementor, Searches and post grid. This is the outline of handling searching in post_meta.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Modify the fields to search in | |
add_action( 'elementor/query/searched_query', function( $query ) { | |
$s = get_search_query(); | |
if ( is_search() ) { | |
$query->set( 'post_type', 'leverandor' ); | |
if( strlen($s) >= 3 ) { | |
// only search ACF fields if more then 3 characters | |
$query->set( 'meta_query', array( | |
'relation' => 'OR', | |
array( | |
'key' => '_post_title_for_search', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'acf_leverandor_short_description', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'acf_leverandor_description', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'acf_leverandor_address_street', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'acf_leverandor_address_street_2', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'acf_leverandor_address_city', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'acf_leverandor_contact_email', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
)); | |
} else { | |
// If less than 3 characters, then only search titles | |
$query->set( 'meta_query', array( | |
'relation' => 'OR', | |
array( | |
'key' => '_post_title_for_search', | |
'value' => $s, | |
'compare' => 'LIKE' | |
), | |
) | |
); | |
} | |
} | |
} ); | |
// Every time a post is saved, update the posts title in a post_meta field so it's searchable | |
// using Meta Queries. It's not the most optimal solution, but it gets around Elementor + SearchWP/Relevanssi not | |
// playing well with Searches for 's' along with Meta Queries | |
add_action( 'save_post', 'yanco_save_post_add_hidden_title_postmeta', 10, 3 ); | |
function yanco_save_post_add_hidden_title_postmeta( $post_id, $post, $update ) { | |
if( $update === false ) { | |
return; | |
} | |
if( $post->post_type !== 'leverandor' ) { | |
return; | |
} | |
remove_action( 'save_post', 'yanco_save_post_add_hidden_title_postmeta', 10, 3 ); | |
// Check for acf_boligforening_subscription_number_new | |
$post_status = get_post_status(); | |
if( $post_status == 'publish' ) { | |
update_field( '_post_title_for_search', get_the_title( $post_id ), $post_id ); | |
} | |
// re-hook this function | |
add_action( 'save_post', 'yanco_save_post_add_hidden_title_postmeta', 10, 3 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment